Russian Doll Envelopes
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)
Example:
Given envelopes =[[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is3([2,3] => [5,4] => [6,7]).
Solution #1 (N^2):
DP solution. Sort the array first based on width, if equal, sort it based on height. Use array to hold max number of dolls can fit into current doll. Iterate from first to last, if any doll before current have a smaller width and height, current = Math.max(current, prev + 1). Record max seen so far for each doll. This is NOT the optimum solution.
public int maxEnvelopes(int[][] envelopes) {
if (envelopes == null || envelopes.length == 0 ||
envelopes[0] == null || envelopes[0].length == 0) {
return 0;
}
Arrays.sort(envelopes, new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b){
if (a[0] != b[0]) {
return a[0] - b[0];
} else {
return a[1] - b[1];
}
}
});
int max = 0;
int[] count = new int[envelopes.length];
Arrays.fill(count, 1);
for (int i = 0; i < envelopes.length; i++) {
for (int j = 0; j < i; j++) {
if (envelopes[i][0] > envelopes[j][0] &&
envelopes[i][1] > envelopes[j][1]){
count[i] = Math.max(count[i], count[j] + 1);
}
}
max = Math.max(count[i], max);
}
return max;
}
Solution #2 (N Log(N)):
Sort the array by: ascending on width, if width is the same, descending on height. Then use binary search to find out the longest increasing subsequence for height of each width.
public int maxEnvelopes(int[][] envelopes) {
if (envelopes == null || envelopes.length == 0 ||
envelopes[0] == null || envelopes[0].length == 0) {
return 0;
}
Arrays.sort(envelopes, new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
if (a[0] != b[0]) {
return a[0] - b[0];
} else {
return b[1] - a[1];
}
}
});
List<Integer> LIS = new ArrayList<Integer>();
for (int i = 0; i < envelopes.length; i++) {
if (LIS.size() == 0 || LIS.get(LIS.size() - 1) < envelopes[i][1]) {
LIS.add(envelopes[i][1]);
}
int left = 0,
right = LIS.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (LIS.get(mid) >= envelopes[i][1]) {
right = mid;
} else {
left = mid + 1;
}
}
LIS.set(right, envelopes[i][1]);
}
return LIS.size();
}
Solution #3 (N Log(N)):
Same methodology in #2. This one doesn't use arraylist, instead using array.
public int maxEnvelopes(int[][] envelopes) {
if(envelopes == null || envelopes.length == 0 ||
envelopes[0] == null || envelopes[0].length != 2) {
return 0;
}
Arrays.sort(envelopes, new Comparator<int[]>() {
public int compare(int[] arr1, int[] arr2) {
if(arr1[0] == arr2[0]) {
return arr2[1] - arr1[1];
}
else {
return arr1[0] - arr2[0];
}
}
});
int dp[] = new int[envelopes.length];
int len = 0;
for(int[] envelope : envelopes) {
int index = Arrays.binarySearch(dp, 0, len, envelope[1]);
if(index < 0) {
index = -(index + 1);
}
dp[index] = envelope[1];
if(index == len) {
len++;
}
}
return len;
}