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 allstable mountains in
any order.
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.
classSolution {
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
classSolution {
funfindStableMountains(height: IntArray, threshold: Int): List<Int> {
val ans = mutableListOf<Int>()
for (i in1 until height.size) {
if (height[i-1] > threshold) ans.add(i)
}
return ans
}
}
1
2
3
4
5
6
7
classSolution:
deffindStableMountains(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 {
pubfnfind_stable_mountains(height: Vec<i32>, threshold: i32) -> Vec<i32> {
letmut ans = Vec::new();
for i in1..height.len() {
if height[i-1] > threshold {
ans.push(i asi32);
}
}
ans
}
}