Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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] <= 16Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Stack
[9,9,6,0,6,6,9]
[6,6,6]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1124: Longest Well-Performing Interval
class Solution {
public int longestWPI(int[] hours) {
int ans = 0, s = 0;
Map<Integer, Integer> pos = new HashMap<>();
for (int i = 0; i < hours.length; ++i) {
s += hours[i] > 8 ? 1 : -1;
if (s > 0) {
ans = i + 1;
} else if (pos.containsKey(s - 1)) {
ans = Math.max(ans, i - pos.get(s - 1));
}
pos.putIfAbsent(s, i);
}
return ans;
}
}
// Accepted solution for LeetCode #1124: Longest Well-Performing Interval
func longestWPI(hours []int) (ans int) {
s := 0
pos := map[int]int{}
for i, x := range hours {
if x > 8 {
s++
} else {
s--
}
if s > 0 {
ans = i + 1
} else if j, ok := pos[s-1]; ok {
ans = max(ans, i-j)
}
if _, ok := pos[s]; !ok {
pos[s] = i
}
}
return
}
# Accepted solution for LeetCode #1124: Longest Well-Performing Interval
class Solution:
def longestWPI(self, hours: List[int]) -> int:
ans = s = 0
pos = {}
for i, x in enumerate(hours):
s += 1 if x > 8 else -1
if s > 0:
ans = i + 1
elif s - 1 in pos:
ans = max(ans, i - pos[s - 1])
if s not in pos:
pos[s] = i
return ans
// Accepted solution for LeetCode #1124: Longest Well-Performing Interval
struct Solution;
use std::collections::HashMap;
impl Solution {
fn longest_wpi(hours: Vec<i32>) -> i32 {
let n = hours.len();
let mut hm: HashMap<i32, usize> = HashMap::new();
let mut score = 0;
let mut res = 0;
for i in 0..n {
score += if hours[i] > 8 { 1 } else { -1 };
if score > 0 {
res = i + 1;
} else {
hm.entry(score).or_insert(i);
if let Some(j) = hm.get(&(score - 1)) {
res = res.max(i - j);
}
}
}
res as i32
}
}
#[test]
fn test() {
let hours = vec![9, 9, 6, 0, 6, 6, 9];
let res = 3;
assert_eq!(Solution::longest_wpi(hours), res);
}
// Accepted solution for LeetCode #1124: Longest Well-Performing Interval
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1124: Longest Well-Performing Interval
// class Solution {
// public int longestWPI(int[] hours) {
// int ans = 0, s = 0;
// Map<Integer, Integer> pos = new HashMap<>();
// for (int i = 0; i < hours.length; ++i) {
// s += hours[i] > 8 ? 1 : -1;
// if (s > 0) {
// ans = i + 1;
// } else if (pos.containsKey(s - 1)) {
// ans = Math.max(ans, i - pos.get(s - 1));
// }
// pos.putIfAbsent(s, i);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
For each element, scan left (or right) to find the next greater/smaller element. The inner scan can visit up to n elements per outer iteration, giving O(n²) total comparisons. No extra space needed beyond loop variables.
Each element is pushed onto the stack at most once and popped at most once, giving 2n total operations = O(n). The stack itself holds at most n elements in the worst case. The key insight: amortized O(1) per element despite the inner while-loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.