Description
Submission
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
map<int, int> Map;
for(auto x: nums) {
Map[x]++;
}
int max_freq = 0;
vector<int> indices;
for(auto iter = Map.begin(); iter != Map.end(); iter++) {
if(iter->second > max_freq) {
max_freq = iter->second;
indices.clear();
indices.push_back(iter->first);
} else if(iter->second == max_freq) {
indices.push_back(iter->first);
}
}
int ret = INT_MAX;
for(auto x: indices) {
int i = 0, j = nums.size() - 1;
for(; nums[i] != x; ++i);
for(; nums[j] != x; --j);
ret = min(ret, j - i + 1);
}
return ret;
}
};