2
public int maxEnvelopes(int[][] envelopes) {3
// sort the envelopes considering only width4
Arrays.sort(envelopes, new sortEnvelopes());6
// Now this is a Longest Increasing Subsequence problem on heights7
// tempList to store the temporary elements, size of this list will be the length of LIS8
ArrayList<Integer> tempList = new ArrayList<>();9
tempList.add(envelopes[0][1]);11
for (int i = 1; i < envelopes.length; i++) {12
if (envelopes[i][1] > tempList.get(tempList.size() - 1)) {13
tempList.add(envelopes[i][1]);15
// if the element is smaller than the largest(last because it is sorted) element of16
// tempList, replace the largest smaller element of tempList with it..17
// ex->(assume if envelopes[i][1] is 4), then >>[1,7,8] will become [1,4,8]<<18
int index = lowerBound(tempList, envelopes[i][1]);19
tempList.set(index, envelopes[i][1]);22
return tempList.size();25
// finding the index of greatest smaller element26
public int lowerBound(ArrayList<Integer> list, int search) {28
int end = list.size() - 1;30
int mid = start + (end - start) / 2;31
if (list.get(mid) < search) {41
class sortEnvelopes implements Comparator<int[]> {42
public int compare(int[] a, int[] b) {44
// to ignore the duplicates, we are sorting such that, for same width-> element with45
// largest height would be considered first, in this way all the other smaller heights would