LeetCode 3547: Smallest Stable Index I — Step-by-Step Visual Trace
Given an integer array and threshold k, find the smallest index where prefix max minus suffix min is within k. Approach Precompute suffix minimums right to left. Scan left to right tracking prefix max. Check the instability score at each index. Time: O(n) · Space: O(n) Code class Solution : def firstStableIndex ( self , nums : list [ int ], k : int ) -> int : n = len ( nums ) if n == 0 : return - 1 suff_min = [ 0 ] * n suff_min [ - 1 ] = nums [ - 1 ] for i in range ( n - 2 , - 1 , - 1 ): suff_mi
Comment
Sign in to join the discussion.
Loading comments…