-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy paths1.cpp
More file actions
23 lines (23 loc) · 675 Bytes
/
Copy paths1.cpp
File metadata and controls
23 lines (23 loc) · 675 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// OJ: https://leetcode.com/problems/degree-of-an-array/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
unordered_map<int, int> cnt, left, right;
int deg = 0, ans = INT_MAX;
for (int i = 0; i < nums.size(); ++i) {
int n = nums[i];
cnt[n]++;
if (cnt[n] > deg) deg = cnt[n];
if (cnt[n] == 1) left[n] = i;
right[n] = i;
}
for (auto p : cnt) {
if (p.second != deg) continue;
ans = min(ans, right[p.first] - left[p.first] + 1);
}
return ans;
}
};