-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDegreeOfAnArray.java
More file actions
54 lines (50 loc) · 1.66 KB
/
DegreeOfAnArray.java
File metadata and controls
54 lines (50 loc) · 1.66 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package array_and_matrix;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: Wenhang Chen
* @Description:给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值。
* <p>
* 你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。
* <p>
* 示例 1:
* <p>
* 输入: [1, 2, 2, 3, 1]
* 输出: 2
* 解释:
* 输入数组的度是2,因为元素1和2的出现频数最大,均为2.
* 连续子数组里面拥有相同度的有如下所示:
* [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
* 最短连续子数组[2, 2]的长度为2,所以返回2.
* 示例 2:
* <p>
* 输入: [1,2,2,3,1,4,2]
* 输出: 6
* 注意:
* <p>
* nums.length 在1到50,000区间范围内。
* nums[i] 是一个在0到49,999范围内的整数。
* @Date: Created in 19:40 2/18/2020
* @Modified by:
*/
public class DegreeOfAnArray {
public int findShortestSubArray(int[] nums) {
Map<Integer, Integer> left = new HashMap(),
right = new HashMap(), count = new HashMap();
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
if (left.get(x) == null) left.put(x, i);
right.put(x, i);
count.put(x, count.getOrDefault(x, 0) + 1);
}
int ans = nums.length;
int degree = Collections.max(count.values());
for (int x : count.keySet()) {
if (count.get(x) == degree) {
ans = Math.min(ans, right.get(x) - left.get(x) + 1);
}
}
return ans;
}
}