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 int findShortestSubArray(int[] nums) {
 3         // maps number with [count, startIndex, endIndex]
 4         HashMap<Integer, int[]> map = new HashMap<Integer, int[]>();
 5
 6         int result = Integer.MAX_VALUE, maxCount = 0;
 7         for (int i = 0; i < nums.length; i++) {
 8             int[] numData = new int[3];
 9             if (map.containsKey(nums[i])) {
10                 int[] temp = map.get(nums[i]);
11                 numData[0] = temp[0] + 1;
12                 numData[1] = temp[1];
13             } else {
14                 numData[0] = 1;
15                 numData[1] = i;
16             }
17             numData[2] = i;
18             map.put(nums[i], numData);
19
20             if (numData[0] > maxCount) {
21                 maxCount = numData[0];
22                 result = numData[2] - numData[1] + 1;
23             } else if (numData[0] == maxCount) {
24                 result = Math.min(result, numData[2] - numData[1] + 1);
25             }
26         }
27
28         return result;
29     }
30 }

原文地址:https://www.cnblogs.com/amazingzoe/p/9082244.html

时间: 2024-11-09 09:56:09

Degree of an 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]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

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-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

V-rep学习笔记:机器人逆运动学数值解法(Damped Least Squares / Levenberg-Marquardt Method)

The damped least squares method is also called the Levenberg-Marquardt method. Levenberg-Marquardt算法是最优化算法中的一种.它是使用最广泛的非线性最小二乘算法,具有梯度法和牛顿法的优点.当λ很小时,步长等于牛顿法步长,当λ很大时,步长约等于梯度下降法的步长. The damped least squares method can be theoretically justified as follo

【C】——幻方算法

一.幻方按照阶数可分成了三类,即奇数阶幻方.双偶阶幻方.单偶阶幻方. 二.奇数阶幻方(劳伯法) 奇数阶幻方最经典的填法是罗伯法.填写的方法是: 把1(或最小的数)放在第一行正中:按以下规律排列剩下的(n×n-1)个数:(1)每一个数放在前一个数的右上一格: (2)如果这个数所要放的格已经超出了顶行那么就把它放在底行,仍然要放在右一列: (3)如果这个数所要放的格已经超出了最右列那么就把它放在最左列,仍然要放在上一行: (4)如果这个数所要放的格已经超出了顶行且超出了最右列,那么就把它放在底行且最