int findShortestSubArray(vector<int>& nums) {
unordered_map<int,int> f;
unordered_map<int,int> start;
unordered_map<int,int> end;
int mx = 0;
for(int i=0; i<nums.size(); i++) {
int n = nums[i];
f[n]++;
if(!start.count(n)) start[n] = i;
end[n] = i;
if(f[n]>mx) mx = f[n];
}
int res = nums.size();
for(auto &m: f) {
if(m.second == mx) {
int len = end[m.first] - start[m.first] + 1;
if(len<res) res = len;
}
}
return res;
}
No comments:
Post a Comment