classSolution{public:intmaxEnvelopes(vector<vector<int>>&envelopes){sort(begin(envelopes),end(envelopes),[](constauto&a,constauto&b){returna[0]==b[0]?a[1]>b[1]:a[0]<b[0];});// Same as 300. Longest Increasing Subsequenceintans=0;vector<int>dp(envelopes.size());for(constvector<int>&e:envelopes){intl=0;intr=ans;while(l<r){constintm=(l+r)/2;if(dp[m]>=e[1])r=m;elsel=m+1;}dp[l]=e[1];if(l==ans)++ans;}returnans;}};
classSolution{publicintmaxEnvelopes(int[][]envelopes){Arrays.sort(envelopes,(a,b)->a[0]==b[0]?b[1]-a[1]:a[0]-b[0]);// Same as 300. Longest Increasing Subsequenceintans=0;int[]dp=newint[envelopes.length];for(int[]e:envelopes){inti=Arrays.binarySearch(dp,0,ans,e[1]);if(i<0)i=-(i+1);dp[i]=e[1];if(i==ans)++ans;}returnans;}}
classSolution:defmaxEnvelopes(self,envelopes:List[List[int]])->int:envelopes.sort(key=lambdax:(x[0],-x[1]))# Same as 300. Longest Increasing Subsequenceans=0dp=[0]*len(envelopes)for_,hinenvelopes:l=0r=answhilel<r:m=(l+r)//2ifdp[m]>=h:r=melse:l=m+1dp[l]=hifl==ans:ans+=1returnans