LeetCode-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 is 3 ([2,3] => [5,4] => [6,7]).

Analysis:

1. DP solution: if i can contain j, then dp[i] = dp[j]+1.

 

2. Longest Increasing Subsequence like solution: we sort the envelopes in the increasing order of width, then we essentially find out the LIS of the array. Note that for i and j with the same width, we need to put the one with smaller height after the other, we won't count it in LIS, i.e., for [7,3] [7,5], arrange like [7,5],[7,3].

 

Solution 1: n^2

 1 public class Solution {
 2     public int maxEnvelopes(int[][] envelopes) {
 3         if (envelopes.length == 0)
 4             return 0;
 5 
 6         Arrays.sort(envelopes, new java.util.Comparator<int[]>() {
 7             public int compare(int[] a, int[] b) {
 8                 if (a[0] == b[0])
 9                     return Integer.compare(a[1], b[1]);
10                 return Integer.compare(a[0], b[0]);
11             }
12         });
13         int[] num = new int[envelopes.length];
14         Arrays.fill(num, 1);
15 
16         int res = 1;
17         for (int i=1;i<envelopes.length;i++){
18             for (int j=i-1;j>=0;j--)
19                 if (num[i] < num[j]+1 && (envelopes[i][0]> envelopes[j][0] && envelopes[i][1] > envelopes[j][1])){ 
20                     num[i] = num[j]+1;
21                 }
22             res = Math.max(res, num[i]);
23         }
24 
25         return res;
26     }
27     
28    
29 }

 

Solution 2: nlogn

 1 public class Solution {
 2     public int maxEnvelopes(int[][] envelopes) {
 3         if (envelopes.length == 0)
 4             return 0;
 5 
 6         Arrays.sort(envelopes, new java.util.Comparator<int[]>() {
 7             public int compare(int[] a, int[] b) {
 8                 if (a[0] == b[0])
 9                     return Integer.compare(b[1], a[1]);
10                 return Integer.compare(a[0], b[0]);
11             }
12         });
13         int[] tails = new int[envelopes.length + 1];
14         tails[0] = 0;
15 
16         int res = 0;
17         for (int i = 1; i < envelopes.length; i++) {
18             int pos = fitPos(envelopes, tails, envelopes[i][1], res);
19             if (pos != -1)
20                 tails[pos] = i;
21             if (pos > res)
22                 res = pos;
23         }
24 
25         return res + 1;
26     }
27 
28     public int fitPos(int[][] envelopes, int[] tails, int height, int end) {
29         int start = 0;
30         while (start <= end) {
31             int mid = start + (end - start) / 2;
32             if (height == envelopes[tails[mid]][1]) {
33                 return -1;
34             } else if (height > envelopes[tails[mid]][1]) {
35                 start = mid + 1;
36             } else {
37                 end = mid - 1;
38             }
39         }
40         return start;
41     }
42     
43    
44 }

 

posted @ 2016-08-01 09:30  LiBlog  阅读(174)  评论(0编辑  收藏  举报