题意
题目链接
把多个信封套在一起,宽和高严格小于,问最多能几个
思路
宽重小到大排序,宽相等时从大往小排,防止同宽放多个信封。
然后对高进行LIS(最长上升子序列)
代码
class Solution {
public:
int maxEnvelopes(vector<vector<int>>& envelopes) {
sort(envelopes.begin(), envelopes.end(),
[](vector<int>& a, vector<int>&b)->bool{return a[0] != b[0] ? a[0] < b[0] : a[1] > b[1];});
vector<int> top;
for (int i = 0; i < envelopes.size(); i++)
{
const int index = envelopes[i][1];
int l = 0, r = top.size();
while (l < r)
{
const int mid = (l + r) >> 1;
if (top[mid] >= index)
r = mid;
else
l = mid + 1;
}
if (l == top.size())
top.push_back(index);
else
top[l] = index;
}
return top.size();
}
};