-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path354_RussianDollEnvelopes354.java
76 lines (69 loc) · 2.37 KB
/
354_RussianDollEnvelopes354.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
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
/**
* You have a number of envelopes with widths and heights given as a pair of
* integers (w, h). One envelope can fit into another if and only if both the
* width and height of one envelope is greater than the width and height of
* the other envelope.
*
* What is the maximum number of envelopes can you Russian doll? (put one inside other)
*
* Note:
* Rotation is not allowed.
*
* Example:
* Input: [[5,4],[6,4],[6,7],[2,3]]
* Output: 3
* Explanation: The maximum number of envelopes you can Russian doll
* is 3 ([2,3] => [5,4] => [6,7]).
*/
public class RussianDollEnvelopes354 {
public int maxEnvelopes(int[][] envelopes) {
if (envelopes == null || envelopes.length == 0) return 0;
Comparator<int[]> comp = new Comparator<int[]>() {
@Override
public int compare(int[] e1, int[] e2) {
int wDiff = Integer.compare(e2[0], e1[0]);
if (wDiff != 0) return wDiff;
return -Integer.compare(e2[1], e1[1]);
}
};
Arrays.sort(envelopes, comp);
int N = envelopes.length;
int[] dp = new int[N];
int res = 1;
for (int i=0; i<N; i++) {
dp[i] = 1;
for (int j=0; j<i; j++) {
if (isLarger(envelopes[j], envelopes[i]) && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
}
}
if (dp[i] > res) res = dp[i];
}
return res;
}
private boolean isLarger(int[] e1, int[] e2) {
return e1[0] > e2[0] && e1[1] > e2[1];
}
public int maxEnvelopes2(int[][] envelopes) {
if (envelopes == null || envelopes.length == 0) return 0;
Comparator<int[]> comp = new Comparator<int[]>() {
@Override
public int compare(int[] e1, int[] e2) {
int wDiff = Integer.compare(e1[0], e2[0]);
if (wDiff != 0) return wDiff;
return Integer.compare(e2[1], e1[1]);
}
};
Arrays.sort(envelopes, comp);
int N = envelopes.length;
int[] dp = new int[N];
int res = 0;
for (int i=0; i<N; i++) {
int index = Arrays.binarySearch(dp, 0, res, envelopes[i][1]);
if (index < 0) index = - index - 1;
dp[index] = envelopes[i][1];
if (index == res) res++;
}
return res;
}
}