#3285

Find Indices of Stable Mountains

Easy
ArrayArrayTwo Pointers
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

The optimal solution leverages a single pass through the array, checking each mountain's stability based on the previous mountain's height. This is efficient and straightforward.

⚙️

Algorithm

5 steps
  1. 1Step 1: Initialize an empty list to store stable mountain indices.
  2. 2Step 2: Loop through the mountains starting from index 1 to the end of the array.
  3. 3Step 3: For each mountain, check if the height of the previous mountain is greater than the threshold.
  4. 4Step 4: If it is, add the current index to the list of stable mountains.
  5. 5Step 5: Return the list of stable mountain indices.
solution.py6 lines
1def find_stable_mountains(height, threshold):
2    stable_indices = []
3    for i in range(1, len(height)):
4        if height[i - 1] > threshold:
5            stable_indices.append(i)
6    return stable_indices

Complexity note: The time complexity is O(n) because we loop through the array once. The space complexity is O(n) due to the storage of stable indices in a list.

  • 1Stable mountains depend on the height of the previous mountain.
  • 2Only mountains from index 1 onward can be stable.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.