-
Notifications
You must be signed in to change notification settings - Fork 1
/
3rd_January.java
42 lines (41 loc) · 1.11 KB
/
3rd_January.java
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
class Solution {
public int removeStudents(int[] H, int N) {
// code here
int m=0;
m=longestSubsequence(N,H);
int ans=0;
ans=N-m;
return (ans);
}
static int longestSubsequence(int size, int arr[])
{
// using binary search
ArrayList<Integer> ans = new ArrayList<>();
ans.add(arr[0]);
int len = 1;
for (int i = 1; i < size; i++) {
if (arr[i] > ans.get(ans.size() - 1)) {
ans.add(arr[i]);
len++;
} else {
int indx = binarySearch(ans, arr[i]);
ans.set(indx, arr[i]);
}
}
return len;
}
static int binarySearch(ArrayList<Integer> ans, int key) {
int low = 0;
int high = ans.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (ans.get(mid) == key) return mid;
else if (ans.get(mid) < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high + 1;
}
};