Longest Well-Performing Interval

Problem Id: 1124 Difficulty: Medium


Intuition

Solution


class Solution:
    def longestWPI(self, hours: List[int]) -> int:
        hours = [(1 if h > 8 else -1) for h in hours]
        s = 0
        length = 0
        min_i = {0: -1}
        for i, h in enumerate(hours):
            s += h
            if s > 0:
                length = i + 1
            if s not in min_i:
                min_i[s] = i
            if s - 1 in min_i:
                length = max(length, i - min_i[s - 1])
        return length