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.
Example 1:
Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].
Example 2:
Input: hours = [6,6,6] Output: 0
Constraints:
1 <= hours.length <= 1040 <= hours[i] <= 16When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find the longest period where there were more good days than bad days. The simplest way to do this is to just check every possible period and see if it meets the criteria.
Here's how the algorithm would work step-by-step:
def longest_well_performing_interval_brute_force(hours):
max_length = 0
number_of_days = len(hours)
for start_index in range(number_of_days):
for end_index in range(start_index, number_of_days):
# Iterate through all possible intervals
good_days_count = 0
bad_days_count = 0
for day_index in range(start_index, end_index + 1):
if hours[day_index] > 8:
good_days_count += 1
else:
bad_days_count += 1
# We need to check if this is a 'good' interval
if good_days_count > bad_days_count:
interval_length = end_index - start_index + 1
max_length = max(max_length, interval_length)
return max_lengthWe're looking for the longest stretch of days where there are more tiring days than non-tiring days. The trick is to transform the problem and use a running total to keep track of whether we're ahead in tiring days.
Here's how the algorithm would work step-by-step:
def longest_well_performing_interval(days):
running_total = 0
first_seen_total = {0: -1}
max_interval_length = 0
for day_index, day in enumerate(days):
if day > 8:
running_total += 1
else:
running_total -= 1
#Record the first occurrence of each running total.
if running_total not in first_seen_total:
first_seen_total[running_total] = day_index
#If we find a smaller running total,
#it means we've found a potentially longer interval.
for previous_total in sorted(first_seen_total.keys()):
if running_total > previous_total:
interval_length = day_index - first_seen_total[previous_total]
max_interval_length = max(max_interval_length, interval_length)
break
return max_interval_length| Case | How to Handle |
|---|---|
| Empty or null input array | Return 0 since there is no interval to evaluate. |
| Input array of length 1 | Return 0, as a single element cannot form a well-performing interval. |
| Array with all elements representing days with more tiring hours (all values > 0) | The entire array is a well-performing interval, so return the array's length. |
| Array with all elements representing days with fewer tiring hours (all values <= 0) | Return 0, as no well-performing interval exists. |
| Array with alternating tiring and non-tiring days | Prefix sum approach should correctly calculate the longest well-performing interval even with alternating patterns. |
| Large input array causing potential integer overflow in prefix sum | Use `long` type for prefix sums if the number of days is large, preventing integer overflow. |
| Input array with very large length, nearing maximum input size. | Verify that memory usage remains acceptable and doesn't exceed reasonable limits (e.g., check the size of auxiliary data structures). |
| Multiple intervals with the same maximum length. | The algorithm should find the length of *one* of the longest intervals, not all of them. |