Longest Well-Performing Interval - Problem

You are a workplace efficiency analyst tracking employee work hours over a period of days. Given an array hours representing the number of hours worked each day by an employee, you need to find the longest well-performing interval.

Here's what makes this interesting:

  • A day is considered tiring if the employee works more than 8 hours (> 8)
  • A day is considered non-tiring if the employee works 8 hours or less (≀ 8)
  • A well-performing interval is any consecutive sequence of days where there are strictly more tiring days than non-tiring days

Your task is to return the length of the longest such interval.

Example: If hours = [9,9,6,0,6,6,9], the interval from index 0 to 2 has 2 tiring days and 1 non-tiring day, making it well-performing with length 3.

Input & Output

example_1.py β€” Standard Case
$ Input: [9,9,6,0,6,6,9]
β€Ί Output: 3
πŸ’‘ Note: The longest well-performing interval is [9,9,6] from index 0 to 2. It has 2 tiring days (9,9) and 1 non-tiring day (6), so tiring > non-tiring with length 3.
example_2.py β€” Single Element
$ Input: [9]
β€Ί Output: 1
πŸ’‘ Note: Single tiring day forms a well-performing interval of length 1 since 1 tiring day > 0 non-tiring days.
example_3.py β€” No Solution
$ Input: [6,6,6]
β€Ί Output: 0
πŸ’‘ Note: All days are non-tiring (≀8 hours). No interval can have more tiring days than non-tiring days, so the answer is 0.

Visualization

Tap to expand
🎯 Work Performance Energy TrackerDaily Hours β†’ Energy Conversion9 hoursTiring Day+16 hoursRest Day-1Energy Timeline: [9,9,6,0,6,6,9]9+11Day 09+12Day 16-11Day 20-10Day 36-1-1Day 4Best Energy Streak: Days 0-2 (Length 3)πŸ—ΊοΈ Hash Map StrategyTrack First Time Each Energy Level Reached:Energy = 1 first seen at Day 0Energy = 2 first seen at Day 1Energy = 0 first seen at Day 3Energy = -1 first seen at Day 4Finding Longest Positive Streak:β€’ When energy > 0: streak from startβ€’ When energy ≀ 0: look for (energy-1) in mapπŸ’‘ Key InsightThe hash map helps us instantly find the earliest point where we can startcounting to get a positive energy gain, maximizing our streak length.πŸ”΄ Tiring Days (>8h)πŸ”΅ Rest Days (≀8h)🟣 Cumulative Energy
Understanding the Visualization
1
Energy Transformation
Convert work hours to energy: >8 hours = +1 energy, ≀8 hours = -1 energy
2
Running Energy Score
Keep track of cumulative energy at each day
3
Find Best Streak
Use hash map to remember when you first hit each energy level
4
Calculate Maximum
Find the longest period with positive net energy
Key Takeaway
🎯 Key Insight: Transform the problem into finding the longest subarray with positive sum using the +1/-1 conversion, then leverage prefix sums with hash map lookups for O(n) efficiency.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through array with O(1) hash map operations

n
2n
βœ“ Linear Growth
Space Complexity
O(n)

Hash map can store at most n different prefix sum values

n
2n
⚑ Linearithmic Space

Constraints

  • 1 ≀ hours.length ≀ 104
  • 0 ≀ hours[i] ≀ 16
  • A tiring day is when hours[i] > 8
  • Well-performing means tiring days > non-tiring days in the interval
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 25
89.3K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen