697. Degree of an Array



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.
 1 class Solution {
 2 public:
 3     int findShortestSubArray(vector<int>& nums) {
 4         unordered_map<int, pair<int, int>> m1;
 5         unordered_map<int, int> m2;
 6         int count = 0,res = nums.size();
 7         for (int i = 0; i < nums.size(); i++)
 8         {
 9             m2[nums[i]]++;
10             if (m1.find(nums[i]) == m1.end()) m1[nums[i]] = pair<int, int>(i, i);
11             else m1[nums[i]].second = i;
12             if (m2[nums[i]] > count) count = m2[nums[i]];
13         }
14         for (auto num : m2)
15         {
16             if (count == num.second)
17             {
18                 res = min(res, m1[num.first].second - m1[num.first].first + 1);
19             }
20         }
21         return res;
22     }
23 };
时间: 2024-10-09 18:16:58

697. Degree of an Array的相关文章

697. Degree of an Array - LeetCode

Description: 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 ha

leetcode 697.Degree of an Array

题意是指在数组中取得出现频率最高的数,然后计算数之间的间隔的. 那么这里可以简单的使用map映射关系来求解的,并且统计出现的次数以及对应的位置关系的. class Solution { public: int findShortestSubArray(vector<int>& nums) { map<int,int> m; map<int,pair<int,int>> pos; int degree=0,res=INT_MAX; for(int i=

LeetCode # Array # Easy # 697 Degree of an Array

题意:给定一个数组,数组的度是其中出现最多次数的元素.求,最小连续子数组的度和原数组一致. 思路:(参考最佳答案) 遍历数组,找到数组最大值,然后根据最大值设置三个长度为max+1的数组left[],right[],counts[],分别用于存储一个数第一次出现的索引.最后一次出现的索引.出现次数.然后,根据counts找到数组的度,再根据right-left求出最小的子数组长度. 1 public class Solution { 2 public int findShortestSubArr

[leetcode]Array-697. Degree of an Array

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 de

leetcode 之 Degree of an Array

1.题目描述 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

Degree of an Array

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 de

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

leetCode-Degree of an Array

Description: 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 ha

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio