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 <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We are given a set of numbers that represent the height of a mountain range. To find 'stable mountains' using brute force, we'll check every possible combination of starting and ending points to see if they form a stable mountain.
Here's how the algorithm would work step-by-step:
def find_stable_mountains_brute_force(heights):
stable_mountain_indices = []
number_of_heights = len(heights)
for start_index in range(number_of_heights):
for end_index in range(start_index, number_of_heights):
# Need at least 3 points to form a mountain
if end_index - start_index + 1 >= 3:
sub_array = heights[start_index:end_index + 1]
is_stable = is_mountain(sub_array)
#If subarray is a stable mountain, store indices
if is_stable:
stable_mountain_indices.append((start_index, end_index))
return stable_mountain_indices
def is_mountain(sub_array):
increasing = True
peak_found = False
if len(sub_array) < 3:
return False
for i in range(1, len(sub_array)):
if increasing:
#Determine if reached the peak
if sub_array[i] < sub_array[i - 1]:
increasing = False
peak_found = True
elif sub_array[i] == sub_array[i - 1]:
return False
else:
#Check if decreasing after the peak
if sub_array[i] >= sub_array[i - 1]:
return False
# The mountain must have a peak
if not peak_found:
return False
return not increasingThe goal is to quickly find the starting points of 'mountains' within a sequence, focusing on sections that first increase and then decrease. We avoid unnecessary checks by efficiently identifying these stable mountain patterns. This involves identifying increasing sections and then checking for the peak and decreasing section that completes the mountain.
Here's how the algorithm would work step-by-step:
def find_stable_mountains(sequence):
mountain_indices = []
index = 0
while index < len(sequence) - 1:
# Attempt to find an increasing section.
start_of_increase = index
while index < len(sequence) - 1 and sequence[index] < sequence[index + 1]:
index += 1
# A peak must exist to continue.
if start_of_increase == index:
index += 1
continue
# Find the peak of the mountain.
peak_index = index
# Find a decreasing section after the peak.
while index < len(sequence) - 1 and sequence[index] > sequence[index + 1]:
index += 1
# Decreasing section must exist after peak to be a mountain
if peak_index == index:
index += 1
continue
mountain_indices.append(start_of_increase)
index += 1
return mountain_indices| Case | How to Handle |
|---|---|
| Null input array | Throw an IllegalArgumentException or return an empty list to avoid NullPointerException |
| Input array of length less than 3 | Return an empty list as a stable mountain requires at least 3 elements |
| Array with all elements equal | Return an empty list, since there will be no peak in an array with all equal values |
| Large array with monotonically increasing or decreasing values | The peak finding algorithm must efficiently identify the absence of a peak and avoid worst-case O(n) iteration |
| Integer overflow when calculating differences between elements | Use long or double data types for intermediate calculations to prevent overflow issues. |
| Input array contains negative numbers | The peak finding and stability check algorithms should correctly handle negative number inputs. |
| Input contains maximum integer values | Ensure comparisons and calculations handle Integer.MAX_VALUE without overflow or unexpected behavior. |
| Multiple stable mountains exist in the input array | The algorithm should identify and return all the starting indices of each valid stable mountain. |