Skip to content

Commit 90b2d5a

Browse files
committed
uploaded solution for 1671
1 parent 0abaac6 commit 90b2d5a

File tree

1 file changed

+36
-0
lines changed
  • Daily Problems/Hard/1671. Minimum Number of Removals to Make Mountain Array

1 file changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from bisect import *
2+
3+
class Solution:
4+
def minimumMountainRemovals(self, nums: list[int]) -> int:
5+
n = len(nums)
6+
7+
# Calculate LIS from the left side
8+
left_lis = [0] * n
9+
temp = []
10+
for i in range(n):
11+
pos = bisect_left(temp, nums[i])
12+
if pos == len(temp):
13+
temp.append(nums[i])
14+
else:
15+
temp[pos] = nums[i]
16+
left_lis[i] = pos + 1
17+
18+
# Calculate LIS from the right side (for LDS)
19+
right_lis = [0] * n
20+
temp = []
21+
for i in range(n - 1, -1, -1):
22+
pos = bisect_left(temp, nums[i])
23+
if pos == len(temp):
24+
temp.append(nums[i])
25+
else:
26+
temp[pos] = nums[i]
27+
right_lis[i] = pos + 1
28+
29+
# Calculate the maximum mountain length
30+
max_mountain = 0
31+
for i in range(1, n - 1):
32+
if left_lis[i] >= 2 and right_lis[i] >= 2:
33+
max_mountain = max(max_mountain, left_lis[i] + right_lis[i] - 1)
34+
35+
# Return minimum elements to remove
36+
return n - max_mountain if max_mountain > 0 else n

0 commit comments

Comments
 (0)