Skip to content

Commit 57a3c12

Browse files
authored
Create longest-well-performing-interval.py
1 parent f9cd13d commit 57a3c12

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def longestWPI(self, hours):
6+
"""
7+
:type hours: List[int]
8+
:rtype: int
9+
"""
10+
result, accu = 0, 0
11+
lookup = {}
12+
for i, h in enumerate(hours):
13+
accu = accu+1 if h > 8 else accu-1
14+
if accu > 0:
15+
result = i+1
16+
elif accu-1 in lookup:
17+
# lookup[accu-1] is the leftmost idx with smaller accu,
18+
# because from 1 to some positive k,
19+
# lookup[accu-i] is a strickly increasing sequence
20+
result = max(result, i-lookup[accu-1])
21+
lookup.setdefault(accu, i)
22+
return result

0 commit comments

Comments
 (0)