-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.py
26 lines (21 loc) · 825 Bytes
/
solution.py
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
class Solution:
def minimumMountainRemovals(self, nums: List[int]) -> int:
n = len(nums)
LIS = [1] * n
LDS = [1] * n
# Compute LIS for each index
for i in range(n):
for j in range(i):
if nums[i] > nums[j]:
LIS[i] = max(LIS[i], LIS[j] + 1)
# Compute LDS from each index
for i in range(n - 1, -1, -1):
for j in range(n - 1, i, -1):
if nums[i] > nums[j]:
LDS[i] = max(LDS[i], LDS[j] + 1)
maxMountainLength = 0
# Find the maximum mountain length
for i in range(1, n - 1):
if LIS[i] > 1 and LDS[i] > 1: # Valid peak
maxMountainLength = max(maxMountainLength, LIS[i] + LDS[i] - 1)
return n - maxMountainLength