Find Indices of Stable Mountains
EasyUpdated: Aug 2, 2025
Practice on:
Problem
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.
Examples
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 than `threshold == 2`.
* Mountain 4 is stable because `height[3] == 4` is greater than `threshold == 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 <= 1001 <= height[i] <= 1001 <= threshold <= 100
Solution
Method 1 – Simple Linear Scan
Intuition
The main idea is to check each mountain (from index 1 onwards) and see if the previous mountain's height is strictly greater than the threshold. If so, that mountain is stable.
Approach
- Initialize an empty list to store the indices of stable mountains.
- Iterate from index 1 to n-1:
- For each index i, check if height[i-1] > threshold.
- If true, add i to the result list.
- Return the result list.
Code
C++
class Solution {
public:
vector<int> findStableMountains(vector<int>& height, int threshold) {
vector<int> ans;
for (int i = 1; i < height.size(); ++i) {
if (height[i-1] > threshold) ans.push_back(i);
}
return ans;
}
};
Go
func findStableMountains(height []int, threshold int) []int {
ans := []int{}
for i := 1; i < len(height); i++ {
if height[i-1] > threshold {
ans = append(ans, i)
}
}
return ans
}
Java
class Solution {
public List<Integer> findStableMountains(int[] height, int threshold) {
List<Integer> ans = new ArrayList<>();
for (int i = 1; i < height.length; i++) {
if (height[i-1] > threshold) ans.add(i);
}
return ans;
}
}
Kotlin
class Solution {
fun findStableMountains(height: IntArray, threshold: Int): List<Int> {
val ans = mutableListOf<Int>()
for (i in 1 until height.size) {
if (height[i-1] > threshold) ans.add(i)
}
return ans
}
}
Python
class Solution:
def findStableMountains(self, height: list[int], threshold: int) -> list[int]:
ans = []
for i in range(1, len(height)):
if height[i-1] > threshold:
ans.append(i)
return ans
Rust
impl Solution {
pub fn find_stable_mountains(height: Vec<i32>, threshold: i32) -> Vec<i32> {
let mut ans = Vec::new();
for i in 1..height.len() {
if height[i-1] > threshold {
ans.push(i as i32);
}
}
ans
}
}
TypeScript
class Solution {
findStableMountains(height: number[], threshold: number): number[] {
const ans: number[] = [];
for (let i = 1; i < height.length; i++) {
if (height[i-1] > threshold) ans.push(i);
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n), where n is the length of height. We check each mountain once. - 🧺 Space complexity:
O(1)(excluding output), as only a result list is used for output.