Leetcode Problem 1124. Longest Well-Performing Interval

1124. Longest Well-Performing Interval

Leetcode Solutions

Using Prefix Sum and HashMap

  1. Initialize score to 0 and res (result) to 0.
  2. Create a hashmap seen to store the first occurrence of each score.
  3. Iterate through the hours array: a. Increment score by 1 if the current day is tiring, else decrement by 1. b. If score is positive, update res to the current index + 1. c. If score is not in seen, add it with the current index. d. If score - 1 is in seen, update res to the maximum of res and the difference between the current index and the index stored in seen for score - 1.
  4. Return res.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...