-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1671. Minimum number of removals to make mountain array
58 lines (42 loc) · 1.41 KB
/
1671. Minimum number of removals to make mountain array
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
//1671. Minimum number of removals to make mountain array
class Solution {
private List<Integer> lisLength(int[] nums) {
List<Integer> lis = new ArrayList<>();
lis.add(nums[0]);
List<Integer> lisLen = new ArrayList<>();
for(int i = 0; i < nums.length; i++) {
lisLen.add(1);
}
for(int i = 1; i < nums.length; i++) {
if(nums[i] > lis.get(lis.size() - 1)) {
lis.add(nums[i]);
}
else {
int index = Collections.binarySearch(lis, nums[i]);
if(index < 0) {
index = -(index + 1);
}
lis.set(index, nums[i]);
}
lisLen.set(i, lis.size());
}
return lisLen;
}
public int minimumMountainRemovals(int[] nums) {
int n = nums.length;
List<Integer> lis = lisLength(nums);
int[] reversed = new int[n];
for(int i = 0; i < n; i++) {
reversed[i] = nums[n - 1 - i];
}
List<Integer> lds = lisLength(reversed);
Collections.reverse(lds);
int removals = Integer.MAX_VALUE;
for(int i = 0; i < n; i++) {
if(lis.get(i) > 1 && lds.get(i) > 1) {
removals = Math.min(removals, n + 1 - lis.get(i) - lds.get(i));
}
}
return removals;
}
}