455. Assign Cookies

来源:互联网 发布:airserver windows 编辑:程序博客网 时间:2024/05/18 07:14

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
简单,先排序,再遍历就是了。

class Solution {public:    int findContentChildren(vector<int>& g, vector<int>& s) {        sort(g.begin(),g.end());        sort(s.begin(),s.end());        auto p1=g.cbegin();        auto p2=s.cbegin();        int ans=0;        while(p1!=g.cend()&&p2!=s.cend()){            if(*p1<=*p2){                ++p1;                ++p2;                ++ans;                            }             else {                ++p2;                            } }        return ans;    }};
0 0
原创粉丝点击