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 Overview: You receive an integer array height representing mountain heights and a value threshold. A mountain is considered stable if its height is greater than the threshold and also strictly greater than the height immediately before it. The task is to return all indices that satisfy this stability condition.
Approach 1: Iterative Linear Scan (O(n) time, O(1) space)
The most direct solution is a single pass through the array. Start from index 1 because the first element has no previous mountain to compare against. For each index i, check two conditions: height[i] > threshold and height[i] > height[i-1]. When both conditions are true, append the index to the result list. This works because each mountain only depends on its immediate neighbor, so no extra data structures are required. The algorithm performs one comparison with the threshold and one comparison with the previous element for every index, leading to O(n) time and constant auxiliary space.
This pattern appears frequently in array problems where decisions depend on adjacent elements. A simple loop with constant memory keeps the implementation clean and efficient.
Approach 2: Functional Programming (Map/Filter) (O(n) time, O(n) space)
Languages like Python and JavaScript allow a more declarative approach using functional utilities such as filter, map, or list comprehensions. Instead of explicitly managing a loop, you generate candidate indices and filter them based on the stability condition. The filtering predicate checks the same two rules: the mountain height must exceed the threshold and be greater than the previous height.
Although the underlying complexity remains O(n) time, functional pipelines typically allocate intermediate structures or iterate over generated sequences, which can lead to O(n) auxiliary space in practice. This approach is concise and expressive but less memory-efficient than the direct loop. It still relies on the same adjacency comparison pattern common in array traversal and simple conditional filtering.
Recommended for interviews: The iterative linear scan is what interviewers expect. It shows you recognize that only the previous element matters, so the problem reduces to a single pass with constant extra space. Mentioning the functional version demonstrates familiarity with modern language features, but the loop-based solution is clearer and closer to the optimal implementation.
This approach involves iterating through the heights starting from the second element (index 1) since the first mountain cannot be stable. For each mountain at index i, check if the height of the previous mountain (height[i-1]) is greater than the given threshold. If it is, add the current index i to the list of stable mountains.
This C solution allocates memory for the resultant array and iterates through the heights array from index 1. For each element, it checks if the previous element is greater than the threshold. If so, it appends the index to the result. Finally, it prints the stable mountain indices and frees the allocated memory.
Time Complexity: O(n), where n is the number of mountains.
Space Complexity: O(n) for storing the result.
This approach leverages functional programming paradigms, using mapping and filtering to achieve the same results more concisely, as supported by the language. We create mappings of possible indices and filter them according to our stability condition.
In this Python solution, we use the filter function along with a lambda to iterate over index range from 1 to len(height), keeping only those indices where the previous mountain's height is greater than the threshold.
Python
JavaScript
Time Complexity: O(n).
Space Complexity: O(n).
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 height. Ignoring the space consumption of the result array, the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Iterative Approach | Time Complexity: O(n), where |
| Functional Programming Approach (Using Map/Filter) | Time Complexity: O(n). |
| Traversal | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Linear Scan | O(n) | O(1) | Best general solution. Single pass over the array with constant memory. |
| Functional Map/Filter | O(n) | O(n) | When writing concise Python or JavaScript code using functional style utilities. |
LeetCode#3285 Find Indices of Stable Mountains - Python • CodeJulian • 1,461 views views
Watch 9 more video solutions →Practice Find Indices of Stable Mountains with our built-in code editor and test cases.
Practice on FleetCode