-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestWPI.cpp
More file actions
25 lines (24 loc) · 907 Bytes
/
longestWPI.cpp
File metadata and controls
25 lines (24 loc) · 907 Bytes
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
class Solution {
public:
int longestWPI(vector<int>& hours) {
int n = hours.size(), ans = 0;
vector<int> prefix(n + 1);
stack<int> st;
st.push(prefix[0] = 0);
for (int i = 1; i <= n; ++i) {
prefix[i] = prefix[i - 1] + (hours[i - 1] > 8 ? 1 : -1);
if (prefix[i] < prefix[st.top()]) st.push(i);
}
for (int i = n; i; --i) {
while (!st.empty() && prefix[i] > prefix[st.top()]) {
ans = std::max(ans, i - st.top());
st.pop();
}
}
return ans;
}
};
// 作者:灵茶山艾府
// 链接:https://leetcode.cn/problems/longest-well-performing-interval/solutions/2110211/liang-chong-zuo-fa-liang-zhang-tu-miao-d-hysl/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。