leetcode 697. Degree of an Array 等于数组的阶的最短的数组 + 遍历即可
2017-12-23 18:16
375 查看
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:
Input: [1, 2, 2, 3, 1]
Output: 2
Explanation:
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.
Example 2:
Input: [1,2,2,3,1,4,2]
Output: 6
Note:
nums.length will be between 1 and 50,000.
nums[i] will be an integer between 0 and 49,999.
这道题给了我们一个数组,定义数组的度为某个或某些数字出现最多的次数,要我们找最短的子数组使其和原数组拥有相同的度。那么我们肯定需要统计每个数字出现的次数,就要用哈希表来建立每个数字和其出现次数之间的映射。由于我们要求包含原度的最小长度的子数组,那么最好的情况就是子数组的首位数字都是统计度的数字,即出现最多的数字。那么我们肯定要知道该频率最大的数字的第一次出现的位置和最后一次出现的位置,由于我们开始不知道哪些数字会出现最多次,所以我们统计所有数字的首尾出现位置,那么我们再用一个哈希表,建立每个数字和其首尾出现的位置。我们用变量degree来表示数组的度。好,现在我们遍历原数组,累加当前数字出现的次数,当某个数字是第一次出现,那么我们用当前位置的来更新该数字出现的首尾位置,否则只更新尾位置。每遍历一个数,我们都更新一下degree。当遍历完成后,我们已经有了数组的度,还有每个数字首尾出现的位置,下面就来找出现次数为degree的数组,然后计算其首尾位置差加1就是candidate数组的长度,由于出现次数为degree的数字不一定只有一个,我们遍历所有的,找出其中最小的即可,
代码如下:
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:
Input: [1, 2, 2, 3, 1]
Output: 2
Explanation:
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.
Example 2:
Input: [1,2,2,3,1,4,2]
Output: 6
Note:
nums.length will be between 1 and 50,000.
nums[i] will be an integer between 0 and 49,999.
这道题给了我们一个数组,定义数组的度为某个或某些数字出现最多的次数,要我们找最短的子数组使其和原数组拥有相同的度。那么我们肯定需要统计每个数字出现的次数,就要用哈希表来建立每个数字和其出现次数之间的映射。由于我们要求包含原度的最小长度的子数组,那么最好的情况就是子数组的首位数字都是统计度的数字,即出现最多的数字。那么我们肯定要知道该频率最大的数字的第一次出现的位置和最后一次出现的位置,由于我们开始不知道哪些数字会出现最多次,所以我们统计所有数字的首尾出现位置,那么我们再用一个哈希表,建立每个数字和其首尾出现的位置。我们用变量degree来表示数组的度。好,现在我们遍历原数组,累加当前数字出现的次数,当某个数字是第一次出现,那么我们用当前位置的来更新该数字出现的首尾位置,否则只更新尾位置。每遍历一个数,我们都更新一下degree。当遍历完成后,我们已经有了数组的度,还有每个数字首尾出现的位置,下面就来找出现次数为degree的数组,然后计算其首尾位置差加1就是candidate数组的长度,由于出现次数为degree的数字不一定只有一个,我们遍历所有的,找出其中最小的即可,
代码如下:
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> using namespace std; class Solution { public: int findShortestSubArray(vector<int>& nums) { map<int, int> count; map<int, pair<int, int>> pos; int maxDegree = 0; for (int i = 0; i < nums.size(); i++) { count[nums[i]]++; if (count[nums[i]] == 1) pos[nums[i]] = { i,i }; else pos[nums[i]].second = i; maxDegree = max(maxDegree, count[nums[i]]); } int minLen = nums.size(); for (auto i : count) { if (maxDegree == i.second) { int len = pos[i.first].second - pos[i.first].first + 1; minLen = min(minLen, len); } } return minLen; } };
相关文章推荐
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
- leetcode 598. Range Addition II 范围内的操作 + 遍历即可
- 题目:输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字。 要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。 例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,
- LeetCode-581. Shortest Unsorted Continuous Subarray (JAVA)最短未排序数组
- leetcode 699. Falling Squares 俄罗斯方块的最高高度 + 暴力遍历即可
- [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
- leetcode -- 58. Length of Last Word【遍历数组的次序:前后】
- LeetCode 15 3Sum 找出数组里面3个数的和等于指定值。
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
- leetcode数组遍历技巧
- LeetCode--581. Shortest Unsorted Continuous Subarray(最短的无序数组长度)Python
- LeetCode-581:Shortest Unsorted Continuous Subarray (最短未排序连续子数组) -- easy
- JS一个非常经典的问题:在遍历数组时对DOM监听事件,索引值将始终等于遍历结束后的值
- JS一个非常经典的问题:在遍历数组时对DOM监听事件,索引值将始终等于遍历结束后的值
- leetcode 498. Diagonal Traverse 矩阵对角遍历 + 控制方向即可
- leetcode之求数组中俩个值和等于给定值用unordered_map降低时间复杂度
- leetcode-java.T001_TwoSum 给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字
- [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
- LeetCode 560. Subarray Sum Equals K (子数组之和等于K)