← ALL NOTES
Array

3904. Smallest Stable Index II

The problem folds the instant you notice nothing needs recomputing: max(nums[0..i]) only grows as i moves right, and min(nums[i..n-1]) is a suffix minimum you can lay down in one backward pass. Carry a running prefix maximum forward, subtract the precomputed suffix minimum, and the first index where that gap falls to k or below is the smallest stable index.

Loading…