## 题目描述

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.

## 解法一

### Python

``````class Solution:
def findShortestSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
counts = {}
starts = {}
ends = {}
max_count = -sys.maxsize
for i, num in enumerate(nums):
if num not in starts:
starts[num] = i
counts[num] = 0
counts[num] += 1
ends[num] = i
max_count = max(max_count, counts[num])
result = sys.maxsize
for num, count in counts.items():
if count == max_count:
result = min(result, ends[num] - starts[num] + 1)
return result``````

### Java

``````class Solution {
public int findShortestSubArray(int[] nums) {
Map<Integer, Integer> counts = new HashMap<>();
Map<Integer, Integer> starts = new HashMap<>();
Map<Integer, Integer> ends = new HashMap<>();
int maxCount = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (!starts.containsKey(num)) {
starts.put(num, i);
counts.put(num, 0);
}
counts.put(num, counts.get(num) + 1);
ends.put(num, i);
maxCount = Math.max(maxCount, counts.get(num));
}
int result = Integer.MAX_VALUE;
for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
if (entry.getValue() == maxCount) {
result = Math.min(result, ends.get(entry.getKey()) - starts.get(entry.getKey()) + 1);
}
}
return result;
}
}``````

### C++

``````class Solution {
public:
int findShortestSubArray(vector<int> &nums) {
unordered_map<int, int> counts;
unordered_map<int, int> starts;
unordered_map<int, int> ends;
int maxCount = INT_MIN;
for (int i = 0; i < nums.size(); i++) {
int num = nums[i];
if (starts.count(num) == 0) {
starts[num] = i;
counts[num] = 0;
}
counts[num]++;
ends[num] = i;
maxCount = max(maxCount, counts[num]);
}
int result = INT_MAX;
for (auto &item : counts) {
if (item.second == maxCount) {
result = min(result, ends[item.first] - starts[item.first] + 1);
}
}
return result;
}
};``````