LC.1124. Longest Well-Performing Interval

在这里插入图片描述

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
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页