Advertisement

Longest Well-Performing Interval - LeetCode 1124 Solution

Longest Well-Performing Interval - Complete Solution Guide

Longest Well-Performing Interval is LeetCode problem 1124, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 interva

Detailed Explanation

The problem asks us to find the longest interval of days within a given list of working hours such that the number of tiring days (days with more than 8 hours worked) is strictly greater than the number of non-tiring days. We are given an array `hours` where each element represents the number of hours worked on a given day. The goal is to return the length of the longest such interval. If no such interval exists, we return 0.

Solution Approach

The provided solution uses a prefix sum approach combined with a hash map to store the first occurrence of each prefix sum. The `score` variable tracks the running sum of tiring days (+1) and non-tiring days (-1). The `seen_scores` hash map stores the first index where each score is seen. For each index `i`, we update the `score`. If the `score` is positive, the interval [0, i] is a candidate for the longest well-performing interval. Otherwise, we check if `score - 1` exists in `seen_scores`. If it does, it means there was a previous index 'k' where the score was `score - 1`. The interval (k, i] would then have a sum of `score - (score - 1) = 1`, making it a well-performing interval. We update `max_length` accordingly.

Step-by-Step Algorithm

  1. Step 1: Initialize `score` to 0 and `max_length` to 0.
  2. Step 2: Initialize a hash map `seen_scores` to store the first index of each score. Add the initial score of 0 at index -1 to handle edge cases.
  3. Step 3: Iterate through the `hours` array from index 0 to n-1.
  4. Step 4: For each day, update the `score`. If `hours[i] > 8`, increment `score` by 1. Otherwise, decrement `score` by 1.
  5. Step 5: If `score > 0`, update `max_length` to `i + 1` because [0, i] is a well-performing interval.
  6. Step 6: If `score <= 0`, check if `score - 1` is present in `seen_scores`. If it is, update `max_length` to `max(max_length, i - seen_scores[score - 1])`.
  7. Step 7: If the current `score` is not already in `seen_scores`, add it to the `seen_scores` along with its index `i`.
  8. Step 8: After iterating through all the days, return `max_length`.

Key Insights

  • Insight 1: Convert the problem to a prefix sum problem. Represent tiring days as +1 and non-tiring days as -1. A well-performing interval is then equivalent to a subarray with a positive sum.
  • Insight 2: If the overall score (prefix sum) is positive at index 'i', then the interval [0, i] is a well-performing interval. This gives us a candidate for the longest well-performing interval.
  • Insight 3: To find a well-performing interval ending at index 'i' when the prefix sum at 'i' is non-positive, look for a previous index 'k' such that `score[i] - score[k] > 0`, which simplifies to `score[k] < score[i]`. To maximize the interval length (i - k), we need to find the smallest such 'k'.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Stack, Monotonic Stack, Prefix Sum.

Companies

Asked at: Infosys.