1
+ class Solution {
2
+ public int minimumMountainRemovals (int [] nums ) {
3
+ // Explanation:: LIS ::
4
+ //Since we need to find the no. of deletion to make mountain array::
5
+ // So if we apply LIS from front and back add the max valueandreduce -1:
6
+ // Eg : [2,1,1,5,6,2,3,1]
7
+ // front:1,1,1,2,3,2,3,1
8
+ // back :2,1,1,3,4,2,2,1
9
+ //------------------------
10
+ // 7-1 =6
11
+ //Note: Botonic array can't be montain arr: mountains must have dec & ase
12
+ // part:
13
+
14
+ // return total len-mountain : no. of deletion:
15
+
16
+
17
+ //*IMP :: Point If we have Increasing party only or decreasing part only
18
+ // not valid one::
19
+ // since when ever we have lis==1 && lds ==1 we can skip it..
20
+
21
+ // TC : O(n^2)
22
+ // SC : O(n)+O(n)
23
+ //return solveLIS(nums);
24
+
25
+ //Lets think of optimise::
26
+ // TC : O(nlogn)
27
+ //SC : O(n)+O(n)
28
+ return solve (nums );
29
+
30
+
31
+
32
+
33
+ }
34
+ public static int solveLIS (int [] arr ){
35
+ int n =arr .length ;
36
+ int flis [] =new int [n ];
37
+ int blis [] =new int [n ];
38
+ Arrays .fill (flis ,1 );
39
+ Arrays .fill (blis ,1 );
40
+
41
+ for (int i =1 ;i <n ;i ++){
42
+ for (int j =0 ;j <i ;j ++){
43
+ if (arr [i ]>arr [j ] && 1 +flis [j ]>flis [i ]){
44
+ flis [i ]=1 +flis [j ];
45
+ }
46
+ }
47
+ }
48
+ int maxi =0 ;
49
+ for (int i =n -1 ;i >=0 ;i --){
50
+ for (int j =n -1 ;j >i ;j --){
51
+ if (arr [i ]>arr [j ] && 1 +blis [j ]>blis [i ]){
52
+ blis [i ]=1 +blis [j ];
53
+ }
54
+ }
55
+ // imp: case it can't be just increasing or decresing::
56
+ //ex : 9 8 1 7 6 5 4 3 2 1
57
+ // lis:1 1 1 2 2 2 2 2 2 1
58
+ // lds:9 8 1 7 6 5 4 3 2 1
59
+ //--------------------------
60
+ //lets understand ::
61
+ // 9 8 7 6 5 4 3 2 1 -- general : not valid since it decresing part
62
+ // only :: mountain array must have both part ::
63
+ if (flis [i ]>1 && blis [i ]>1 ){
64
+ maxi =Math .max (maxi ,flis [i ]+blis [i ]-1 );
65
+ }
66
+
67
+ }
68
+ return n -maxi ;
69
+ }
70
+ public static int solve (int [] nums ){
71
+ int n =nums .length ;
72
+ int lis [] =new int [n ];
73
+ int lds [] =new int [n ];
74
+
75
+
76
+ ArrayList <Integer > temp =new ArrayList <>();
77
+
78
+ //LIS::
79
+ temp .add (nums [0 ]);
80
+ for (int i =1 ;i <n ;i ++){
81
+ if (temp .get (temp .size ()-1 )<nums [i ]){
82
+ temp .add (nums [i ]);
83
+ }else {
84
+ int pos = binarySearch (nums [i ],temp );
85
+ temp .set (pos ,nums [i ]);
86
+ }
87
+ lis [i ] =temp .size ();
88
+ }
89
+
90
+ temp .clear ();
91
+ //LDS::
92
+ temp .add (nums [n -1 ]);
93
+ for (int i =n -2 ;i >=0 ;i --){
94
+ if (temp .get (temp .size ()-1 )<nums [i ] ){
95
+ temp .add (nums [i ]);
96
+ }else {
97
+ int pos = binarySearch (nums [i ],temp );
98
+ temp .set (pos ,nums [i ]);
99
+ }
100
+ lds [i ] =temp .size ();
101
+
102
+ }
103
+ int maxi =0 ;
104
+ for (int i =0 ;i <n ;i ++){
105
+ // skip the completely incresing / decresing case::)
106
+ if (lis [i ]>1 && lds [i ]>1 ){
107
+ maxi =Math .max (maxi ,lis [i ]+lds [i ]-1 );
108
+ }
109
+ }
110
+ return nums .length -maxi ;
111
+
112
+ }
113
+ public static int binarySearch (int target ,ArrayList <Integer > arr ){
114
+ int l =0 ;
115
+ int h =arr .size ()-1 ;
116
+
117
+ while (l <=h ){
118
+ int m =l +(h -l )/2 ;
119
+ if (arr .get (m )==target ){
120
+ return m ;
121
+ }else if (arr .get (m )>target ){
122
+ h =m -1 ;
123
+ }else {
124
+ l =m +1 ;
125
+ }
126
+ }
127
+ return l ;
128
+ }
129
+ }
0 commit comments