LeetCode 3285: Find Indices of Stable Mountains

Array

Problem Description

Explanation

To solve this problem, we iterate through the array of mountain heights, starting from index 1. For each mountain, we check if its height is greater than the threshold and if the previous mountain's height is also greater than the threshold. If both conditions are met, we consider the mountain stable and add its index to the result array.

Algorithm:

  1. Initialize an empty list to store the indices of stable mountains.
  2. Iterate through the array starting from index 1.
  3. Check if the current mountain's height is greater than the threshold and the previous mountain's height is also greater than the threshold.
  4. If both conditions are met, add the index of the current mountain to the result list.
  5. Return the list of indices of stable mountains.

Time Complexity

The time complexity of this algorithm is O(n), where n is the number of mountains.

Space Complexity

The space complexity is O(1) since we only use a constant amount of extra space.

Solutions

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<Integer> findStableMountains(int[] height, int threshold) {
        List<Integer> result = new ArrayList<>();

        for (int i = 1; i < height.length; i++) {
            if (height[i] > threshold && height[i - 1] > threshold) {
                result.add(i);
            }
        }

        return result;
    }
}

Loading editor...