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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments