-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1124.py
36 lines (27 loc) · 1.03 KB
/
p1124.py
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
# https://leetcode.cn/problems/longest-well-performing-interval/description/
import unittest
from typing import List
# 单调栈
class Solution:
@staticmethod
def longestWPI(hours: List[int]) -> int:
n: int = len(hours)
presum: list[int] = [0] * (n + 1) # 前缀和
stack: list[int] = [0]
for i, hour in enumerate(hours, 1):
presum[i] = presum[i - 1] + (1 if hour > 8 else -1)
# 维护单调递减栈
if presum[i] < presum[stack[-1]]:
stack.append(i)
ans: int = 0
for j in range(n, 0, -1):
# 当前前缀和>栈顶前缀和,表示劳累天数>不劳累天数
while stack and presum[j] > presum[stack[-1]]:
ans = max(ans, j - stack.pop())
return ans
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(Solution.longestWPI([9, 9, 6, 0, 6, 6, 9]), 3)
self.assertEqual(Solution.longestWPI([6, 6, 6]), 0)
if __name__ == "__main__":
unittest.main()