-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCodeQ1671.java
More file actions
41 lines (38 loc) · 1.18 KB
/
leetCodeQ1671.java
File metadata and controls
41 lines (38 loc) · 1.18 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package DynamicProgramming ;
public class leetCodeQ1671 {
public static int minimumMountainRemovals(int[] nums) {
int n = nums.length;
// LIS :- Left to Right
int[] dp1 = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i - 1; j++) {
if (nums[j] < nums[i])
dp1[i] = Math.max(dp1[i], dp1[j]);
}
dp1[i] += 1;
}
// LIS :- Right to Left
int[] dp2 = new int[n];
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j <= n - 1; j++) {
if (nums[j] < nums[i])
dp2[i] = Math.max(dp2[i], dp2[j]);
}
dp2[i] += 1;
}
int maxLen = 0;
for (int i = 0; i < n; i++) {
if (dp1[i] > 1 && dp2[i] > 1) {
int len = dp1[i] + dp2[i] - 1;
maxLen = Math.max(maxLen, len);
}
}
if (maxLen < 3)
return 0;
return n - maxLen;
}
public static void main(String[] args) {
int[] nums = { 2, 1, 1, 5, 6, 2, 3, 1 } ;
System.out.println(minimumMountainRemovals(nums));
}
}