diff --git a/354_Max Envelopes b/354_Max Envelopes new file mode 100644 index 0000000..23ff946 --- /dev/null +++ b/354_Max Envelopes @@ -0,0 +1,25 @@ +public static int maxEnvelopes(int[][] arr) { + + Arrays.sort(arr,(a, b)->{ + // 0'th index-> width and 1'st index -> height, + // for C++: sort(arr.begin(),arr.end(),[](vector& a,vector& b){ //logic }); + if(a[0]==b[0]) return b[1] - a[1]; // reverse sort. for c++ replace '-' with '<' + else return a[0]-b[0]; // default sort. for c++ replace '-' with '<' + }); + + int n=arr.length; + int[] dp=new int[n]; + int maxLen = 0; + for (int i =0;i arr[j][1]) { // compare height. + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + maxLen = Math.max(maxLen, dp[i]); + } + + return maxLen; + } +