-
Notifications
You must be signed in to change notification settings - Fork 820
/
RussianDollEnvelopes.java
72 lines (67 loc) · 2.08 KB
/
RussianDollEnvelopes.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
/* (C) 2024 YourCompanyName */
package dynamic_programming;
import java.util.*;
/**
* Created by gouthamvidyapradhan on 05/05/2019 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.
*
* <p>What is the maximum number of envelopes can you Russian doll? (put one inside other)
*
* <p>Note: Rotation is not allowed.
*
* <p>Example:
*
* <p>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]).
*
* <p>Solution: O(N ^ 2) Sort the envelopes based on increasing order of area and for each envelope
* iterate through all the possible envelopes which are smaller than that the current envelope and
* check the maximum possible envelopes which an be russian dolled.
*/
public class RussianDollEnvelopes {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
int[][] A = {{5, 4}, {6, 4}, {6, 7}, {2, 3}};
System.out.println(new RussianDollEnvelopes().maxEnvelopes(A));
}
class Envelope {
int l, b;
Envelope(int l, int b) {
this.l = l;
this.b = b;
}
}
/**
* @param envelopes
* @return
*/
public int maxEnvelopes(int[][] envelopes) {
if (envelopes.length == 0) return 0;
List<Envelope> list = new ArrayList<>();
for (int[] row : envelopes) {
list.add(new Envelope(row[0], row[1]));
}
list.sort(((o1, o2) -> Integer.compare(o2.l * o2.b, o1.l * o1.b)));
int[] DP = new int[envelopes.length];
Arrays.fill(DP, 1);
for (int i = list.size() - 1; i >= 0; i--) {
Envelope env = list.get(i);
for (int j = i + 1, l = list.size(); j < l; j++) {
Envelope childEnv = list.get(j);
if (env.l > childEnv.l && env.b > childEnv.b) {
DP[i] = Math.max(DP[i], DP[j] + 1);
}
}
}
int ans = 1;
for (int i : DP) {
ans = Math.max(ans, i);
}
return ans;
}
}