-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
697.java
59 lines (57 loc) · 1.99 KB
/
697.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
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
55
56
57
58
59
__________________________________________________________________________________________________
sample 3 ms submission
class Solution {
public int findShortestSubArray(int[] nums) {
if (nums.length == 1) return 1;
int[] indexs = new int[50000];
int[] degree = new int[50000];
for (int i = 0; i < 50000; i++) {
indexs[i] = -1;
}
int max = 1, len = 1;
for (int i = 0; i < nums.length; i++) {
if (indexs[nums[i]] == -1) {
indexs[nums[i]] = i;
}
degree[nums[i]]++;
if (degree[nums[i]] > max) {
max = degree[nums[i]];
len = i - indexs[nums[i]] + 1;
} else if (degree[nums[i]] == max && len > i - indexs[nums[i]] + 1) {
len = i - indexs[nums[i]] + 1;
}
}
return len;
}
}
__________________________________________________________________________________________________
sample 40104 kb submission
class Solution {
public int findShortestSubArray(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
Map<Integer, int[]> map = new HashMap<>();
for(int i=0; i<nums.length; i++) {
if(!map.containsKey(nums[i])) {
map.put(nums[i], new int[]{1, i, i});
} else {
int[] temp = map.get(nums[i]);
temp[0]++;
temp[2] = i;
}
}
int degree = Integer.MIN_VALUE;
int result = Integer.MAX_VALUE;
for(int[] value : map.values()) {
if(value[0] > degree) {
degree = value[0];
result = value[2] - value[1] + 1;
} else if(value[0] == degree) {
result = Math.min(result, value[2] - value[1] + 1);
}
}
return result;
}
}
__________________________________________________________________________________________________