LeetCode 3549: Smallest Stable Index II — Step-by-Step Visual Trace
Same problem as Q1 but with larger constraints (up to 10^5). The O(n) approach with suffix min and prefix max is required. Approach Precompute suffix minimums. Scan left to right with a running prefix maximum. Return the first index where prefix_max - suffix_min . 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 ,
Comment
Sign in to join the discussion.
Loading comments…