-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindShortestSubArray.cpp
More file actions
39 lines (37 loc) · 1.06 KB
/
findShortestSubArray.cpp
File metadata and controls
39 lines (37 loc) · 1.06 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
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
unordered_map<int, int> cnt, first;
int degree = 0, len = 0;
for(int i = 0; i < nums.size(); ++i){
int e = nums[i];
cnt[e]++;
if(!first.count(e)) first[e] = i;
if(cnt[e] > degree || (cnt[e] == degree && i - first[e] + 1 < len)) {
degree = cnt[e];
len = i - first[e] + 1;
}
}
return len;
}
};
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
unordered_map<int, int> m, left, right;
int degree = 0, ans = nums.size();
for (int i = 0; i < ans; i++) {
int e = nums[i];
if (m.find(e) == m.end())
left[e] = i;
right[e] = i;
m[e]++;
degree = std::max(degree, m[e]);
}
for (auto &it: m) {
if (it.second == degree)
ans = std::min(ans, right[it.first] - left[it.first] + 1);
}
return ans;
}
};