You have a number of envelopes with widths and heights given as a pair of integers (w, h)
. One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.
What is the maximum number of envelopes can you Russian doll? (put one inside other)
Note:
Rotation is not allowed.
struct object
{
bool operator()(pair<int, int> &a, pair<int, int> &b)
{
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
}
}myobject;
class Solution {
public:
typedef bool (Solution::*ltype)(pair<int, int>, pair<int, int>);
int maxEnvelopes(vector<pair<int, int>>& envelopes) {
int ans = 0;
int size = envelopes.size();
//lter = &Solution::myfunction;
sort(envelopes.begin(),envelopes.end(),myobject);
vector<int> dp;
for(int i=0;i<size;i++)
{
auto it = lower_bound(dp.begin(),dp.end(),envelopes[i].second);
if(it == dp.end()) dp.push_back(envelopes[i].second);
else *it = envelopes[i].second;
}
return dp.size();
}
private:
/* static bool myfunction(pair<int, int> a, pair<int, int> b)
{
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
}
*/
};
class Solution {
public:
typedef bool (Solution::*ltype)(pair<int, int>, pair<int, int>);
int maxEnvelopes(vector<pair<int, int>>& envelopes) {
int ans = 0;
int size = envelopes.size();
//lter = &Solution::myfunction;
sort(envelopes.begin(),envelopes.end(),&Solution::myfunction);
vector<int> dp;
for(int i=0;i<size;i++)
{
auto it = lower_bound(dp.begin(),dp.end(),envelopes[i].second);
if(it == dp.end()) dp.push_back(envelopes[i].second);
else *it = envelopes[i].second;
}
return dp.size();
}
private:
static bool myfunction(pair<int, int> a, pair<int, int> b)
{
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
}
};
这里想借sort函数讲一下函数指针,类成员函数指针,和函数对象的使用;
//sort函数的第三个参数可以接受函数指针和函数对象,但是不接受类成员函数指针。所以这里我两种都试了一下。但是在使用函数指针的时候出了问题,一开始我定义了一个类成员函数指针变量,将&Solution::myfunction的值赋给这个指针,然后将这个指针变量赋给sort函数的第三个参数,编译错误。应该是这个指针变量的类型是类成员函数指针而不是函数指针的关系,变量类型不匹配。而且要将myfunction函数改成静态成员函数,才能赋给sort函数。后来我直接将&Solution::myfunction的值赋给sort函数的第三个参数,编译就正确了。//