-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0845-longest-mountain-in-array.js
47 lines (43 loc) · 1.24 KB
/
0845-longest-mountain-in-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 845. Longest Mountain in Array
* https://leetcode.com/problems/longest-mountain-in-array/
* Difficulty: Medium
*
* You may recall that an array arr is a mountain array if and only if:
* - arr.length >= 3
* - There exists some index i (0-indexed) with 0 < i < arr.length - 1 such that:
* - arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
* - arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
*
* Given an integer array arr, return the length of the longest subarray, which is a
* mountain. Return 0 if there is no mountain subarray.
*/
/**
* @param {number[]} arr
* @return {number}
*/
var longestMountain = function(arr) {
const n = arr.length;
if (n < 3) return 0;
let maxLength = 0;
let i = 1;
while (i < n - 1) {
const isPeak = arr[i] > arr[i - 1] && arr[i] > arr[i + 1];
if (!isPeak) {
i++;
continue;
}
let leftBound = i - 1;
while (leftBound > 0 && arr[leftBound - 1] < arr[leftBound]) {
leftBound--;
}
let rightBound = i + 1;
while (rightBound < n - 1 && arr[rightBound] > arr[rightBound + 1]) {
rightBound++;
}
const mountainLength = rightBound - leftBound + 1;
maxLength = Math.max(maxLength, mountainLength);
i = rightBound + 1;
}
return maxLength;
};