forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
degree-of-an-array.cpp
29 lines (28 loc) · 972 Bytes
/
degree-of-an-array.cpp
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
// Time: O(n)
// Space: O(n)
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
unordered_map<int, int> left, right;
unordered_map<int, int> counts;
for (int i = 0; i < nums.size(); ++i) {
if (left.count(nums[i]) == 0) {
left[nums[i]] = i;
}
right[nums[i]] = i;
++counts[nums[i]];
}
auto degree = max_element(counts.begin(), counts.end(),
[](const pair<int, int>& a,
const pair<int, int>& b) {
return a.second < b.second;
})->second;
auto result = numeric_limits<int>::max();
for (const auto& kvp : counts) {
if (kvp.second == degree) {
result = min(result, right[kvp.first] - left[kvp.first] + 1);
}
}
return result;
}
};