Welcome to Subscribe On Youtube

3903. Smallest Stable Index I

Description

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.

 

Example 1:

Input: nums = [5,0,1,4], k = 3

Output: 3

Explanation:

  • At index 0: The maximum in [5] is 5, and the minimum in [5, 0, 1, 4] is 0, so the instability score is 5 - 0 = 5.
  • At index 1: The maximum in [5, 0] is 5, and the minimum in [0, 1, 4] is 0, so the instability score is 5 - 0 = 5.
  • At index 2: The maximum in [5, 0, 1] is 5, and the minimum in [1, 4] is 1, so the instability score is 5 - 1 = 4.
  • At index 3: The maximum in [5, 0, 1, 4] is 5, and the minimum in [4] is 4, so the instability score is 5 - 4 = 1.
  • This is the first index with an instability score less than or equal to k = 3. Thus, the answer is 3.

Example 2:

Input: nums = [3,2,1], k = 1

Output: -1

Explanation:

  • At index 0, the instability score is 3 - 1 = 2.
  • At index 1, the instability score is 3 - 1 = 2.
  • At index 2, the instability score is 3 - 1 = 2.
  • None of these values is less than or equal to k = 1, so the answer is -1.

Example 3:

Input: nums = [0], k = 0

Output: 0

Explanation:

At index 0, the instability score is 0 - 0 = 0, which is less than or equal to k = 0. Therefore, the answer is 0.

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 109
  • 0 <= k <= 109

Solutions

Solution 1: Preprocessing + Enumeration

First, we preprocess an array $\textit{right}$, where $\textit{right}[i]$ represents the minimum value among the elements in $nums$ from index $i$ to index $n - 1$. We can compute the $\textit{right}$ array by traversing $nums$ from back to front.

Next, we traverse the $nums$ array from front to back, maintaining a variable $\textit{left}$, which represents the maximum value among the elements in $nums$ from index $0$ to index $i$. For each index $i$, we calculate the instability score as $\textit{left} - \textit{right}[i]$. If the instability score is less than or equal to $k$, we return index $i$. If no such index is found after the traversal, we return $-1$.

The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the $nums$ array.

  • class Solution {
        public int firstStableIndex(int[] nums, int k) {
            int n = nums.length;
            int[] right = new int[n];
            right[n - 1] = nums[n - 1];
    
            for (int i = n - 2; i >= 0; i--) {
                right[i] = Math.min(right[i + 1], nums[i]);
            }
    
            int left = 0;
            for (int i = 0; i < n; i++) {
                left = Math.max(left, nums[i]);
                if (left - right[i] <= k) {
                    return i;
                }
            }
            return -1;
        }
    }
    
    
  • class Solution {
    public:
        int firstStableIndex(vector<int>& nums, int k) {
            int n = nums.size();
            vector<int> right(n);
            right[n - 1] = nums[n - 1];
    
            for (int i = n - 2; i >= 0; --i) {
                right[i] = min(right[i + 1], nums[i]);
            }
    
            int left = 0;
            for (int i = 0; i < n; ++i) {
                left = max(left, nums[i]);
                if (left - right[i] <= k) {
                    return i;
                }
            }
            return -1;
        }
    };
    
    
  • class Solution:
        def firstStableIndex(self, nums: list[int], k: int) -> int:
            n = len(nums)
            right = [nums[-1]] * n
            for i in range(n - 2, -1, -1):
                right[i] = min(right[i + 1], nums[i])
            left = 0
            for i, x in enumerate(nums):
                left = max(left, x)
                if left - right[i] <= k:
                    return i
            return -1
    
    
  • func firstStableIndex(nums []int, k int) int {
    	n := len(nums)
    	right := make([]int, n)
    	right[n-1] = nums[n-1]
    
    	for i := n - 2; i >= 0; i-- {
    		right[i] = min(right[i+1], nums[i])
    	}
    
    	left := 0
    	for i, x := range nums {
    		left = max(left, x)
    		if left-right[i] <= k {
    			return i
    		}
    	}
    	return -1
    }
    
    
  • function firstStableIndex(nums: number[], k: number): number {
        const n = nums.length;
        const right = new Array<number>(n);
        right[n - 1] = nums[n - 1];
    
        for (let i = n - 2; i >= 0; i--) {
            right[i] = Math.min(right[i + 1], nums[i]);
        }
    
        let left = 0;
        for (let i = 0; i < n; i++) {
            left = Math.max(left, nums[i]);
            if (left - right[i] <= k) {
                return i;
            }
        }
        return -1;
    }
    
    

All Problems

All Solutions