Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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:
height[2] == 3 is greater than threshold == 2.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 <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,3,4,5] 2
[10,1,10,1,10] 3
[10,1,10,1,10] 10
find-in-mountain-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3285: Find Indices of Stable Mountains
class Solution {
public List<Integer> stableMountains(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;
}
}
// Accepted solution for LeetCode #3285: Find Indices of Stable Mountains
func stableMountains(height []int, threshold int) (ans []int) {
for i := 1; i < len(height); i++ {
if height[i-1] > threshold {
ans = append(ans, i)
}
}
return
}
# Accepted solution for LeetCode #3285: Find Indices of Stable Mountains
class Solution:
def stableMountains(self, height: List[int], threshold: int) -> List[int]:
return [i for i in range(1, len(height)) if height[i - 1] > threshold]
// Accepted solution for LeetCode #3285: Find Indices of Stable Mountains
/**
* [3285] Find Indices of Stable Mountains
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn stable_mountains(height: Vec<i32>, threshold: i32) -> Vec<i32> {
(1..height.len())
.filter_map(|i| {
if height[i - 1] > threshold {
Some(i as i32)
} else {
None
}
})
.collect()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3285() {
assert_eq!(
vec![3, 4],
Solution::stable_mountains(vec![1, 2, 3, 4, 5], 2)
);
assert_eq!(
Vec::<i32>::new(),
Solution::stable_mountains(vec![10, 1, 10, 1, 10], 10)
);
}
}
// Accepted solution for LeetCode #3285: Find Indices of Stable Mountains
function stableMountains(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;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.