3285. Find Indices of Stable Mountains
Description
There are n
mountains in a row, and each mountain has a height. You are given an integer array height
where height[i]
represents the height of mountain i
, and an integer threshold
.
A mountain is called stable if the mountain just before it (if it exists) has a height strictly greater than threshold
. Note that mountain 0 is not stable.
Return an array containing the indices of all stable mountains in any order.
Example 1:
Input: height = [1,2,3,4,5], threshold = 2
Output: [3,4]
Explanation:
- Mountain 3 is stable because
height[2] == 3
is greater thanthreshold == 2
. - Mountain 4 is stable because
height[3] == 4
is greater thanthreshold == 2
.
Example 2:
Input: height = [10,1,10,1,10], threshold = 3
Output: [1,3]
Example 3:
Input: height = [10,1,10,1,10], threshold = 10
Output: []
Constraints:
2 <= n == height.length <= 100
1 <= height[i] <= 100
1 <= threshold <= 100
Solutions
Solution 1: Traversal
We directly traverse the mountains starting from index $1$. If the height of the mountain to its left is greater than $threshold$, we add its index to the result array.
After the traversal, we return the result array.
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{height}$. Ignoring the space consumption of the result array, the space complexity is $O(1)$.
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 |
|