0% completed
Problem Statement
Given an integer array arr
, return true
if arr
is a valid mountain
array. Otherwise, return false
.
The array is called a valid mountain array
if:
arr.length > 2
- There exists some
i
with0 < i < arr.length - 1
such that:arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Examples
Example 1:
- Input: arr =
[4, 5, 6, 7, 8, 7, 6, 5]
- Expected Output:
true
- Justification: The elements increase from
4
to8
and then decrease from8
back to5
, forming a mountain shape.
Example 2:
- Input: arr =
[1, 2, 3, 4, 5]
- Expected Output:
false
- Justification: The elements only increase and there's no decrease after a peak, hence not forming a mountain.
Example 3:
- Input: arr =
[9, 8, 7, 6, 5]
- Expected Output:
false
- Justification: The elements decrease from the start without an initial increase, failing to form a mountain shape.
Solution
To solve this problem, we'll follow a two-phase approach that mirrors the structure of a mountain: ascending and descending. Initially, we'll iterate through the array, ensuring that each element is strictly larger than its predecessor, signifying an upward slope towards the peak. Upon reaching the peak, which is identified when the current element is no longer greater than the next, we transition to the descending phase. In this phase, we ensure that each subsequent element is strictly smaller, representing the downward slope of the mountain.
The algorithm succeeds if it finds both an ascending and descending sequence with at least one element in each, forming a valid mountain shape. This approach is effective because it closely follows the definition of a mountain array, requiring a strict increase followed by a strict decrease, and can be implemented efficiently.
Step-by-Step Algorithm
-
Initialize an Index Variable: Start with an index variable,
i
, set to 0. This variable will be used to iterate through the array. -
Check for Ascending Order:
- While
i+1
is less than the array length and the current element is less than the next element (arr[i] < arr[i + 1]
), incrementi
. - This loop checks for a strictly increasing sequence before the peak.
- While
-
Validate Peak Existence:
- Ensure that the peak is not at the start or end of the array. If
i
is 0 ori
equalsarr.length - 1
, the array does not form a mountain shape, so returnfalse
.
- Ensure that the peak is not at the start or end of the array. If
-
Check for Descending Order:
- After identifying the peak, continue checking the elements. While
i+1
is less than the array length and the current element is greater than the next element (arr[i] > arr[i + 1]
), incrementi
. - This loop ensures the sequence is strictly decreasing after the peak.
- After identifying the peak, continue checking the elements. While
-
Verify End of Array:
- If
i
equalsarr.length - 1
by the end of the process, it means we have a valid mountain array. Returntrue
. - If not, the array does not form a valid mountain shape. Return
false
.
- If
Algorithm Walkthrough
Let's consider the input: arr = [4, 5, 6, 7, 8, 7, 6, 5]
.
-
Initialize
i
as 0. -
Ascending Order Check:
- Step 1:
i = 0
,arr[i] = 4
,arr[i + 1] = 5
. Since4 < 5
, incrementi
to 1. - Step 2:
i = 1
,arr[i] = 5
,arr[i + 1] = 6
. Since5 < 6
, incrementi
to 2. - Step 3:
i = 2
,arr[i] = 6
,arr[i + 1] = 7
. Since6 < 7
, incrementi
to 3. - Step 4:
i = 3
,arr[i] = 7
,arr[i + 1] = 8
. Since7 < 8
, incrementi
to 4.
- Step 1:
-
Validate Peak Existence:
- Now
i = 4
andarr[i] = 8
. The next element is7
, so we stop ascending. Sincei
is not0
orarr.length - 1
, we have a valid peak.
- Now
-
Descending Order Check:
- Step 5:
i = 4
,arr[i] = 8
,arr[i + 1] = 7
. Since8 > 7
, incrementi
to 5. - Step 6:
i = 5
,arr[i] = 7
,arr[i + 1] = 6
. Since7 > 6
, incrementi
to 6. - Step 7:
i = 6
,arr[i] = 6
,arr[i + 1] = 5
. Since6 > 5
, incrementi
to 7.
- Step 5:
-
Verify End of Array:
- Now
i = 7
, which isarr.length - 1
. We have successfully found a valid mountain array shape, so the algorithm returnstrue
.
- Now
Code
Complexity Analysis
Time Complexity: O(n)
The algorithm scans the array at most twice. The first scan is to check for the strictly increasing sequence until the peak is reached, and the second scan checks for the strictly decreasing sequence after the peak. Since both scans are linear and there are no nested iterations, the time complexity is O(n), where n is the length of the array.
Space Complexity: O(1)
The algorithm uses a fixed amount of space. The space used does not scale with the input size, resulting in constant space complexity.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible