-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path697.cpp
More file actions
21 lines (21 loc) · 666 Bytes
/
697.cpp
File metadata and controls
21 lines (21 loc) · 666 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
int n = nums.size();
unordered_map<int,pair<int,int>> ump;
int degree = 0;
int val = -1, li = n+1, ri = -1;
for(int i=0; i<n; i++){
if(!ump.count(nums[i]))
ump[nums[i]] = {0,i};
ump[nums[i]].first += 1;
if(ump[nums[i]].first>degree || (ump[nums[i]].first==degree && ri-li+1>i-ump[nums[i]].second+1)){
degree = ump[nums[i]].first;
val = nums[i];
li = ump[nums[i]].second;
ri = i;
}
}
return ri-li+1;
}
};