-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_354.java
More file actions
35 lines (31 loc) · 953 Bytes
/
Copy pathP_354.java
File metadata and controls
35 lines (31 loc) · 953 Bytes
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
package leetcode.hard;
import java.util.Arrays;
public class P_354 {
public int maxEnvelopes(int[][] envelopes) {
Arrays.sort(envelopes, (a, b) -> a[0] == b[0] ? Integer.compare(b[1], a[1])
: Integer.compare(a[0], b[0]));
final int[] lis = new int[envelopes.length];
int len = 0;
for (int[] envelope : envelopes) {
final int idx = lowerBound(lis, len, envelope[1]);
lis[idx] = envelope[1];
if (len == idx) {
len++;
}
}
return len;
}
private static int lowerBound(int[] arr, int to, int target) {
int lo = 0;
int hi = to;
while (lo < hi) {
final int mid = lo + hi >>> 1;
if (arr[mid] < target) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo;
}
}