class Solution(object):
def longestWPI(self, hours):
"""
贪心思想,最长的子序列和接近1
"""
sumer = 0
dict = {}
res = 0
for index, hour in enumerate(hours):
sumer += 1 if hour > 8 else -1
if sumer > 0:
res = index + 1
# dict存第一次出现的sumer的位置
if sumer not in dict:
dict[sumer] = index
if sumer - 1 in sumer:
res = max(res, index - dict[sumer-1])
return res
LC.1124. Longest Well-Performing Interval
最新推荐文章于 2023-01-31 12:00:28 发布