Leetcode 875 Solution
This article provides solution to leetcode question 875 (longest-mountain-in-array)
Access this page by simply typing in "lcs 875" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/longest-mountain-in-array
Solution
class Solution:
def longestMountain(self, A: List[int]) -> int:
if len(A) < 3:
return 0
dp1 = [0] * len(A)
for i in range(len(A)):
if i == 0:
dp1[0] = 1
else:
dp1[i] = 1 + (dp1[i - 1] if A[i] > A[i - 1] else 0)
dp2 = [0] * len(A)
for i in reversed(range(len(A))):
if i == len(A) - 1:
dp2[i] = 1
else:
dp2[i] = 1 + (dp2[i + 1] if A[i] > A[i + 1] else 0)
ans = 0
for v1, v2 in zip(dp1, dp2):
if v1 > 1 and v2 > 1:
ans = max(ans, v1 + v2 - 1)
return ans