Leetcode 1124: Longest Well-Performing Interval

grid47
grid47
Exploring patterns and algorithms
Jul 17, 2024 6 min read

You are given a list of integers representing the number of hours worked each day. A day is considered tiring if the number of hours worked is strictly greater than 8. A well-performing interval is an interval of days where the number of tiring days is strictly larger than the number of non-tiring days. Your task is to return the length of the longest well-performing interval.
Problem
Approach
Steps
Complexity
Input: You are given a list of integers representing the number of hours worked per day. The length of the list is between 1 and 10,000.
Example: Input: hours = [9,9,6,0,6,6,9]
Constraints:
• The number of elements in hours is between 1 and 10,000.
• Each integer in hours is between 0 and 16.
Output: Return the length of the longest well-performing interval.
Example: Output: 3
Constraints:
• The output should be an integer representing the length of the longest well-performing interval.
Goal: The goal is to find the longest interval where the number of tiring days is greater than the number of non-tiring days.
Steps:
• Traverse the list and calculate a score where tiring days are scored as +1 and non-tiring days as -1.
• Track the cumulative score and use a map to store the earliest index for each score.
• For each score, check if a previous score exists that satisfies the condition of a well-performing interval, and update the result.
Goal: You must ensure that the solution works for a list with up to 10,000 elements efficiently.
Steps:
• The input list will contain between 1 and 10,000 elements.
• Each element in the input list is an integer between 0 and 16.
Assumptions:
• The input list is non-empty.
• The length of the list will be between 1 and 10,000.
Input: Input: hours = [9,9,6,0,6,6,9]
Explanation: In this case, the longest well-performing interval is [9,9,6], as there are 2 tiring days (9,9) and 1 non-tiring day (6).

Input: Input: hours = [6,6,6]
Explanation: There are no tiring days in this list, so there is no well-performing interval.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus