首页 > 代码库 > Leetcode: Russian Doll Envelopes
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]).
DP 解法, Time Complexity: O(N^2)
sort based on width or height, anyone is ok. After the sorting, for each envelope, those envelopes which can fit into this one can only be in the front.
then maintain an array dp[i], where dp[i] is the max number of envelopes that can fit into envelope i.
1 public class Solution { 2 public int maxEnvelopes(int[][] envelopes) { 3 if(envelopes == null || envelopes.length == 0 || envelopes[0] == null || envelopes[0].length != 2) 4 return 0; 5 int maxDoll = Integer.MIN_VALUE; 6 int[] dp = new int[envelopes.length]; 7 Comparator<int[]> comp = new Comparator<int[]>(){ 8 public int compare(int[] arr1, int[] arr2) { 9 return arr1[0] - arr2[0]; 10 } 11 }; 12 Arrays.sort(envelopes, comp); 13 14 for (int i=0; i<dp.length; i++) { 15 dp[i] = 1; //initialize 16 for (int j=0; j<i; j++) { 17 if (envelopes[j][0]<envelopes[i][0] && envelopes[j][1]<envelopes[i][1]) { 18 dp[i] = Math.max(dp[i], dp[j]+1); 19 } 20 } 21 maxDoll = Math.max(maxDoll, dp[i]); 22 } 23 return maxDoll; 24 } 25 }
Better Solution: Also DP, find longest increasing subsequence in the array. Time Complexity: O(NlogN)
- Sort the array. Ascend on width and descend on height if width are same.
- Find the longest increasing subsequence based on height.
Notice:
- Since the width is increasing, we only need to consider height.
- [3, 4] cannot contains [3, 3], so we need to put [3, 4] before [3, 3] when sorting otherwise it will be counted as an increasing number if the order is [3, 3], [3, 4]
Arrays.binarysearch(int[] a, int fromIndex, int toIndex, int key) if not find key int array a, will return -(insertionPosition)-1, The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. toIndex is the index of the last element (exclusive) to be searched. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
1 public int maxEnvelopes(int[][] envelopes) { 2 if(envelopes == null || envelopes.length == 0 3 || envelopes[0] == null || envelopes[0].length != 2) 4 return 0; 5 Arrays.sort(envelopes, new Comparator<int[]>(){ 6 public int compare(int[] arr1, int[] arr2){ 7 if(arr1[0] == arr2[0]) 8 return arr2[1] - arr1[1]; 9 else 10 return arr1[0] - arr2[0]; 11 } 12 }); 13 int dp[] = new int[envelopes.length]; 14 int len = 0; 15 for(int[] envelope : envelopes){ 16 int index = Arrays.binarySearch(dp, 0, len, envelope[1]); 17 if(index < 0) 18 index = -(index + 1); 19 dp[index] = envelope[1]; 20 if(index == len) 21 len++; 22 } 23 return len; 24 }
Leetcode: Russian Doll Envelopes