LeetCode Courses 3903. Smallest Stable Index I
LeetCode

3903. Smallest Stable Index I

We iterate from a brute-force scan to a two-pass sweep that precomputes a right-min array and walks a running left-max. The "precompute one side, sweep the other" pattern crops up everywhere.

1 chapter Beginner Written walk-through Updated 8 May 2026 Phil · @FirnoxGames
Free

Free for everyone. No account needed.

Start reading →

What you'll learn

1 3903. Smallest Stable Index I 📖 14 min read

We iterate from a brute-force scan to a two-pass sweep that precomputes a right-min array and walks a running left-max. The "precompute one side, sweep the other" pattern crops up everywhere.

In this chapter
  1. Problem
  2. Observations
  3. Brute force
  4. Max-optimisation
  5. Counter
  6. Step back
  7. Optimal

Problem

You are given an integer array nums of length n and an integer k.

For each index i, define its instability score as max(nums[0..i]) - min(nums[i..n - 1]).

In other words: - max(nums[0..i]) is the largest value among the elements from index 0 to index i. - min(nums[i..n - 1]) is the smallest value among the elements from index i to index n - 1.

An index i is called stable if its instability score is less than or equal to k.

Return the smallest stable index. If no such index exists, return -1.

Included with this course
📖Written version of every chapter