We are given hours, a list of the number of hours worked per day for a given employee.
A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8.
A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days.
Return the length of the longest well-performing interval.
Function Signature
def longest_well_performing_interval(hours: list[int]) -> int:
Rules
-
An interval is a contiguous, non-empty range of days
hours[i..j]
.
-
A day with exactly 8 hours is not tiring.
-
Return
0
when no well-performing interval exists.
Constraints
-
1 <= len(hours) <= 10^4
-
0 <= hours[i] <= 16
Examples
Input: hours = [9, 9, 6, 0, 6, 6, 9]
Output: 3
The interval [9, 9, 6] has 2 tiring days against 1 non-tiring day. Any longer interval needs at least 3 tiring days, and the only interval holding all three (positions 0, 1 and 6) is the whole list, where 4 days are non-tiring.
Input: hours = [6, 6, 6]
Output: 0
No day is tiring, so no interval qualifies.
Input: hours = [6, 6, 9, 9, 6]
Output: 3
Days 1 to 3 ([6, 9, 9]) and days 2 to 4 ([9, 9, 6]) each have 2 tiring days against 1. There are only 2 tiring days in total, so an interval of 4 or more days cannot have more tiring than non-tiring days.