-
Notifications
You must be signed in to change notification settings - Fork 0
/
1124.longest-well-performing-interval.cpp
59 lines (52 loc) · 1.33 KB
/
1124.longest-well-performing-interval.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @lc app=leetcode id=1124 lang=cpp
*
* [1124] Longest Well-Performing Interval
*/
// @lc code=start
class Solution
{
public:
int longestWPI(vector<int> &hours)
{
// 使用单调栈和前缀和
const int n = hours.size();
int res = 0;
//use a vector convert the problem to presum
// tiring day -> 1
// non tiring day -> -1
vector<int> presum;
presum.reserve(n + 1);
presum.push_back(0); // add one extra 0
int sum = 0; // accumulate presum
for (const auto hr : hours)
{
if (hr > 8)
{
sum += 1;
presum.push_back(sum);
}
else
{
sum += -1;
presum.push_back(sum);
}
}
stack<int> Stack; // pos, mono de.
for (int i = 0; i < presum.size(); ++i)
{
if (Stack.empty() || presum[Stack.top()] > presum[i])
Stack.push(i);
} //for
for (int j = presum.size() - 1; j >= 0; --j)
{
while (!Stack.empty() && presum[Stack.top()] < presum[j])
{
res = max(res, j - Stack.top());
Stack.pop();
} //while
} //for
return res;
}
};
// @lc code=end