-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1671.java
More file actions
76 lines (61 loc) · 1.94 KB
/
LC1671.java
File metadata and controls
76 lines (61 loc) · 1.94 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* LC1671
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LC1671 {
private static List<Integer> LIS(List<Integer> nums) {
int n = nums.size();
List<Integer> lis = new ArrayList<>();
List<Integer> lisLen = new ArrayList<>(Collections.nCopies(n, 1));
for (int i = 0; i < n; i++) {
int num = nums.get(i);
int lb = Collections.binarySearch(lis, num);
if (lb < 0) {
lb = -lb - 1;
}
if (lb >= lis.size()) {
lis.add(num);
} else {
lis.set(lb, num);
}
lisLen.set(i, lis.size());
}
return lisLen;
}
public static int minimumMountainRemovals(int[] nums) {
int n = nums.length;
List<Integer> numList = new ArrayList<>();
for (int num : nums) {
numList.add(num);
}
List<Integer> lisR = LIS(numList);
Collections.reverse(numList);
List<Integer> lisRL = LIS(numList);
Collections.reverse(lisRL);
int maxMountainSize = 0;
for (int i = 0; i < n; i++) {
if (lisR.get(i) > 1 && lisRL.get(i) > 1) {
maxMountainSize = Math.max(maxMountainSize, lisR.get(i) + lisRL.get(i) - 1);
}
}
return n - maxMountainSize;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Array Size : ");
int size = sc.nextInt();
System.out.println();
int[] arr = new int[size];
System.out.println("Enter The Array Elements : ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("[%d] : ", i);
arr[i] = sc.nextInt();
}
System.out.println();
int answer = minimumMountainRemovals(arr);
System.out.println(answer);
sc.close();
}
}