1124表现良好的最长时间段

1124表现良好的最长时间段

解答

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
#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
int longestWPI(vector<int>& hours) {
int n = hours.size();
unordered_map<int, int> count;
int cur = 0;
int res = 0;
for (int i = 0; i < n; ++i) {
if (hours[i] > 8) {
cur++;
} else {
cur--;
}
if (cur > 0) res = i + 1;
else {
if (count.count(cur-1) > 0) res = max(res, i - count[cur-1]);
if (count.count(cur) < 1) count[cur] = i;
}
}
return res;

}
};
-------------本文结束,感谢您的阅读-------------