Imagine you're analyzing a financial time series where you need to find stable pivot points - moments where the trend was declining before and rising after.
Given a 0-indexed integer array nums of size n and a positive integer k, your task is to find all "good indices" that represent these stable points.
An index i is considered good if k โค i < n - k and both conditions are met:
- The
kelements immediately before indexiare in non-increasing order (declining or stable) - The
kelements immediately after indexiare in non-decreasing order (rising or stable)
Goal: Return an array of all good indices sorted in increasing order.
Example: For nums = [2,1,1,1,3,4,1], k = 2, index 3 is good because the 2 elements before it [1,1] are non-increasing and the 2 elements after it [3,4] are non-decreasing.
Input & Output
Visualization
Time & Space Complexity
Three linear passes through the array
Two additional arrays to store precomputed lengths
Constraints
- n == nums.length
- 3 โค n โค 105
- 1 โค nums[i] โค 106
- 1 โค k โค n / 2
- Valid range: k โค i < n - k must be non-empty