-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path697.cpp
More file actions
27 lines (27 loc) · 1.12 KB
/
Copy path697.cpp
File metadata and controls
27 lines (27 loc) · 1.12 KB
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
class Solution {
// Runtime: 28 ms, faster than 98.39% of C++ online submissions for Degree of
// an Array. Memory Usage: 12.4 MB, less than 92.00% of C++ online submissions
// for Degree of an Array.
public:
int findShortestSubArray(vector<int> &nums) {
unordered_map<int, tuple<int, int, int>> hashmap;
for (int i = 0; i < nums.size(); ++i) {
if (auto it = hashmap.find(nums[i]); it != end(hashmap)) {
auto &[first, last, count] = it->second;
last = i;
++count;
} else {
hashmap.insert({nums[i], {i, i, 1}});
}
}
auto ret = max_element(begin(hashmap), end(hashmap),
[](const auto &a, const auto &b) {
if (get<2>(a.second) == get<2>(b.second))
return get<1>(a.second) - get<0>(a.second) >
get<1>(b.second) - get<0>(b.second);
return get<2>(a.second) < get<2>(b.second);
});
return ret == end(hashmap) ? 0
: get<1>(ret->second) - get<0>(ret->second) + 1;
}
};