|
| 1 | +/* |
| 2 | +354. Russian Doll Envelopes |
| 3 | +Hard |
| 4 | +
|
| 5 | +You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope. |
| 6 | +One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height. |
| 7 | +Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other). |
| 8 | +Note: You cannot rotate an envelope. |
| 9 | +
|
| 10 | +Example 1: |
| 11 | +Input: envelopes = [[5,4],[6,4],[6,7],[2,3]] |
| 12 | +Output: 3 |
| 13 | +Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]). |
| 14 | +
|
| 15 | +Example 2: |
| 16 | +Input: envelopes = [[1,1],[1,1],[1,1]] |
| 17 | +Output: 1 |
| 18 | + |
| 19 | +Constraints: |
| 20 | +1 <= envelopes.length <= 105 |
| 21 | +envelopes[i].length == 2 |
| 22 | +1 <= wi, hi <= 105 |
| 23 | +*/ |
| 24 | +class Solution { |
| 25 | + public int maxEnvelopes(int[][] envelopes) { |
| 26 | + // LIS Modified :) |
| 27 | + // If we sort on basic of first and if equal the on basic of second:: |
| 28 | + // Simply apply LIS on both :) |
| 29 | + |
| 30 | + //edge case ::) |
| 31 | + if(envelopes.length==1){ |
| 32 | + return 1; |
| 33 | + } |
| 34 | + //return solveLis(envelopes); |
| 35 | + // TC : O(n^2) --TLE |
| 36 | + // SC: O(n)+O(n) |
| 37 | + // fails at 85/87 testcase:: |
| 38 | + |
| 39 | + // TC : O(nlogn) |
| 40 | + // SC: O(n)+O(n) |
| 41 | + return solveLISOptimised(envelopes); |
| 42 | + |
| 43 | + } |
| 44 | + public static int solveLis(int[][] envelopes){ |
| 45 | + Arrays.sort(envelopes,(a,b)->{ |
| 46 | + if(a[0]==b[0]){ |
| 47 | + return b[1]-a[1]; |
| 48 | + }else{ |
| 49 | + return a[0]-b[0]; |
| 50 | + } |
| 51 | + }); |
| 52 | + int n=envelopes.length; |
| 53 | + |
| 54 | + int len[] = new int[n]; |
| 55 | + Arrays.fill(len,1); |
| 56 | + int maxi=0; |
| 57 | + for(int i=1;i<n;i++){ |
| 58 | + for(int j=0;j<i;j++){ |
| 59 | + if(envelopes[i][0]>envelopes[j][0] && envelopes[i][1]>envelopes[j][1] && 1+len[j]>len[i]){ |
| 60 | + len[i] =1+len[j]; |
| 61 | + } |
| 62 | + } |
| 63 | + maxi =Math.max(len[i],maxi); |
| 64 | + } |
| 65 | + return maxi; |
| 66 | + } |
| 67 | + public static int solveLISOptimised(int[][] envelopes){ |
| 68 | + int n=envelopes.length; |
| 69 | + Arrays.sort(envelopes,(a,b)->{ |
| 70 | + if(a[0]==b[0]){ |
| 71 | + return b[1]-a[1]; |
| 72 | + }else{ |
| 73 | + return a[0]-b[0]; |
| 74 | + } |
| 75 | + }); |
| 76 | + // since the oth indx of every is sorted :: |
| 77 | + // so if we could able to find lis on 1st then that would be the ans:: |
| 78 | + int arr[] =new int[n]; |
| 79 | + int k=0; |
| 80 | + for(int[] ele:envelopes){ |
| 81 | + arr[k++]=ele[1]; |
| 82 | + } |
| 83 | + List<Integer> temp =new ArrayList<>(); |
| 84 | + temp.add(arr[0]); |
| 85 | + for(int i=1;i<n;i++){ |
| 86 | + if(!temp.isEmpty() && temp.get(temp.size()-1)<arr[i]){ |
| 87 | + temp.add(arr[i]); |
| 88 | + }else{ |
| 89 | + int pos =binarySearch(temp,arr[i]); |
| 90 | + temp.set(pos,arr[i]); |
| 91 | + } |
| 92 | + } |
| 93 | + return temp.size(); |
| 94 | + } |
| 95 | + public static int binarySearch(List<Integer> arr,int target){ |
| 96 | + int l=0; |
| 97 | + int h=arr.size()-1; |
| 98 | + while(l<=h){ |
| 99 | + int mi =l+(h-l)/2; |
| 100 | + if(arr.get(mi)==target){ |
| 101 | + return mi; |
| 102 | + }else if(arr.get(mi)>target){ |
| 103 | + h=mi-1; |
| 104 | + }else{ |
| 105 | + l=mi+1; |
| 106 | + } |
| 107 | + } |
| 108 | + return l; |
| 109 | + } |
| 110 | +} |
0 commit comments