↓Skip to main content
  1. LeetCode/

LeetCode 3904: Smallest Stable Index II

·1 min· ·
LeetCode Medium Prefix-Sum Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-04-19

  • Submissions: Accepted (first try)

Approach
#

Each index scores max(nums[0..i]) - min(nums[i..n-1]); scan once from each side (Prefix Sum Template 2):

  • Build the suffix minimum right to left first.
  • Update the prefix maximum left to right, returning the first index scoring <= k.

Versions I and II differ only in the length limit (100 for I, 10^5 for II); I used the same O(n) code for both, though for I’s small range computing max and min directly at every position, O(n²), also passes. See LeetCode 3903 for the other one.

My Solution
#

class Solution:
    def firstStableIndex(self, nums: List[int], k: int) -> int:
        n = len(nums)
        suf_min = nums[:]
        for i in range(n - 2, -1, -1):
            suf_min[i] = min(suf_min[i + 1], nums[i])
        pre_max = -inf
        for i, x in enumerate(nums):
            pre_max = max(pre_max, x)
            if pre_max - suf_min[i] <= k:
                return i
        return -1

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Both ranges include i itself.

Interview Explanation
#

The score is the maximum up to i minus the minimum from i. I build the suffix minimum right to left, then keep the prefix maximum left to right, and the first position with difference <= k is the answer. Time O(n).

Related

LeetCode 3719: Longest Balanced Subarray I
·2 mins
LeetCode Medium Hash-Table Prefix-Sum Segment-Tree Array
LeetCode 3719 note: n is only 1500, so fix the left end and extend right, maintaining distinct odds and evens in two sets
LeetCode 930: Binary Subarrays With Sum
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 930 note: “sum exactly goal” = “sum at most goal” − “sum at most goal - 1”; or prefix sums with a hash map
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 2875: Minimum Size Subarray in Infinite Array
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 2875 note: strip target // sum full cycles; the remainder is smaller than one cycle, so a sliding window over two copies of nums always finds it
LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)