-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsolution1.js
More file actions
45 lines (42 loc) · 785 Bytes
/
solution1.js
File metadata and controls
45 lines (42 loc) · 785 Bytes
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
/**
* https://leetcode-cn.com/problems/longest-mountain-in-array/
*
* 845. 数组中的最长山脉
*
* Medium
*
* 2068ms 12.50%
* 36.6mb 100.00%
*/
const longestMountain = A => {
const max = A.length
let ans = 0
for (let i = 1; i < max - 1; i++) {
ans = Math.max(getMountainLen(A, i), ans)
}
return ans
}
function getMountainLen(A, index) {
const max = A.length
let leftLen = 0
let rightLen = 0
let start = index - 1
let end = index + 1
while (start >= 0 || end < max) {
if (A[start] < A[start + 1]) {
leftLen++
start--
continue
}
if (A[end] < A[end - 1]) {
rightLen++
end++
continue
}
break
}
if (leftLen !== 0 && rightLen !== 0) {
return leftLen + 1 + rightLen
}
return 0
}