题目:
题目链接
思路:排序+LIS
LIS最长上升子序列 算法模板
代码实现
#define ls envelopes
class Solution {
public:
struct ss{
int w,h;
bool operator <(const ss b){
if(w==b.w) return h>b.h;
else return w<b.w;
}
}xf[100005];
int find(int *a,int x,int l,int r){
while(l<r){
int mid=(l+r)>>1;
if(x< a[mid] ) r=mid;
else if(x> a[mid] ) l=mid+1;
else l=r=mid;
}return l;
}
int maxEnvelopes(vector<vector<int>>& envelopes) {
for(int i=0;i<envelopes.size();i++)
xf[i]=(ss){ls[i][0],ls[i][1]};
sort(xf,xf+ls.size());
int low[100005];
int ans=0;low[0]=0;
for(int i=0;i<ls.size();i++)
if(xf[i].h>low[ans]){
low[++ans]=xf[i].h;
}else if(xf[i].h<low[ans]){
int id=find(low,xf[i].h,1,ans);
low[id]=xf[i].h;
}
return ans;
}
};