forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
27 lines (26 loc) · 895 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public int findShortestSubArray(int[] nums) {
Map<Integer, int[]> mapper = new HashMap<>();
for (int i = 0, n = nums.length; i < n; ++i) {
if (mapper.containsKey(nums[i])) {
int[] arr = mapper.get(nums[i]);
++arr[0];
arr[2] = i;
} else {
int[] arr = new int[]{1, i, i};
mapper.put(nums[i], arr);
}
}
int maxDegree = 0, minLen = 0;
for (Map.Entry<Integer, int[]> entry : mapper.entrySet()) {
int[] arr = entry.getValue();
if (maxDegree < arr[0]) {
maxDegree = arr[0];
minLen = arr[2] - arr[1] + 1;
} else if (maxDegree == arr[0]) {
minLen = Math.min(minLen, arr[2] - arr[1] + 1);
}
}
return minLen;
}
}