mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
33 lines
624 B
Go
33 lines
624 B
Go
package leetcode
|
|
|
|
func longestMountain(A []int) int {
|
|
left, right, res, isAscending := 0, 0, 0, true
|
|
for left < len(A) {
|
|
if right+1 < len(A) && ((isAscending == true && A[right+1] > A[left] && A[right+1] > A[right]) || (right != left && A[right+1] < A[right])) {
|
|
if A[right+1] < A[right] {
|
|
isAscending = false
|
|
}
|
|
right++
|
|
} else {
|
|
if right != left && isAscending == false {
|
|
res = max(res, right-left+1)
|
|
}
|
|
left++
|
|
if right < left {
|
|
right = left
|
|
}
|
|
if right == left {
|
|
isAscending = true
|
|
}
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func max(a int, b int) int {
|
|
if a > b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|