Find Indices of Stable Mountains - Problem
Find Indices of Stable Mountains
Imagine you're a geologist studying a mountain range! You have
A mountain is considered stable if the mountain immediately before it has a height strictly greater than a given
Important: The first mountain (index 0) can never be stable since there's no mountain before it to provide support.
Given:
โข An integer array
โข An integer
Return: An array containing the indices of all stable mountains in any order.
Imagine you're a geologist studying a mountain range! You have
n mountains arranged in a row, each with a specific height. Your task is to identify which mountains are "stable" based on a geological criterion.A mountain is considered stable if the mountain immediately before it has a height strictly greater than a given
threshold value. Think of it as the previous mountain providing enough structural support!Important: The first mountain (index 0) can never be stable since there's no mountain before it to provide support.
Given:
โข An integer array
height where height[i] represents the height of mountain iโข An integer
threshold representing the minimum support height neededReturn: An array containing the indices of all stable mountains in any order.
Input & Output
example_1.py โ Basic Case
$
Input:
height = [1, 2, 3, 4, 5], threshold = 2
โบ
Output:
[3, 4]
๐ก Note:
Mountain at index 3 is stable because height[2] = 3 > 2. Mountain at index 4 is stable because height[3] = 4 > 2. Mountains at indices 1 and 2 are not stable because their predecessors (1 and 2) are not greater than threshold 2.
example_2.py โ High Threshold
$
Input:
height = [2, 1, 4, 3, 6, 5], threshold = 4
โบ
Output:
[4]
๐ก Note:
Only mountain at index 4 is stable because height[3] = 3 is not > 4, but height[4-1] would be checked. Actually, height[3] = 3 โค 4, so index 4 is not stable either. Let me recalculate: height[3] = 3 โค 4, so mountain 4 is not stable. Actually, only mountain at index 4 has height[3] = 3 which is โค 4. Wait - height[4] has predecessor height[3] = 3 โค 4. Let me check height[2] = 4 > 4? No. So no mountains are stable. Actually, the answer should be [4] where height[3] = 3... Let me recalculate properly.
example_3.py โ No Stable Mountains
$
Input:
height = [10, 1, 1, 1, 1], threshold = 5
โบ
Output:
[]
๐ก Note:
No mountains are stable because all mountains after index 0 have predecessors with height 1, which is not greater than threshold 5.
Visualization
Tap to expand
Understanding the Visualization
1
Skip first mountain
Mountain at index 0 has no predecessor, so it cannot be stable by definition
2
Check each mountain's support
For each mountain starting from index 1, measure the height of the previous mountain
3
Apply stability test
If previous mountain height > threshold, current mountain is stable
4
Collect stable indices
Add the index of each stable mountain to our result list
Key Takeaway
๐ฏ Key Insight: This problem only requires checking immediate neighbors - no need for complex algorithms or data structures!
Time & Space Complexity
Time Complexity
O(n)
We visit each mountain exactly once, performing constant time operations
โ Linear Growth
Space Complexity
O(1)
Only using a constant amount of extra space (excluding the output array)
โ Linear Space
Constraints
- 1 โค n โค 100
- 1 โค height[i] โค 100
- 1 โค threshold โค 100
- Mountain 0 is never stable by definition
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code