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

1
2
3
4
5
6
7
8
9

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

1
2
3
4

Input: height = [10,1,10,1,10], threshold = 3

Output: [1,3]

Example 3

1
2
3
4

Input: height = [10,1,10,1,10], threshold = 10

Output: []

Constraints

  • 2 <= n == height.length <= 100
  • 1 <= height[i] <= 100
  • 1 <= 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

  1. Initialize an empty list to store the indices of stable mountains.
  2. 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.
  3. Return the result list.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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;
    }
};
1
2
3
4
5
6
7
8
9
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
}
1
2
3
4
5
6
7
8
9
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;
    }
}
1
2
3
4
5
6
7
8
9
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
    }
}
1
2
3
4
5
6
7
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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
    }
}
1
2
3
4
5
6
7
8
9
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.