Longest Well-Performing Interval LeetCode solution in C++

1
class Solution {
2
public:
3
int longestWPI(vector<int> &hours) {
4
int n = hours.size();
5
int ans = 0;
6
int ct = 0;
7
unordered_map<int, int> m;
8
for (int i = 0; i < n; ++i) {
9
if (hours[i] > 8)
10
ct++;
11
else
12
ct--;
13
if (ct > 0)
14
ans = max(ans, i + 1);
15
else {
16
if (m.find(ct) == m.end()) m[ct] = i;
17
if (m.find(ct - 1) != m.end()) ans = max(ans, i - m[ct - 1]);
18
}
19
}
20
return ans;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0