455. Assign Cookies

来源:互联网 发布:node wiki 编辑:程序博客网 时间:2024/05/17 01:20

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());        int count=0;        for(int i=0,j=0;i<g.size()&&j<s.size();j++){            if(g[i]<=s[j]){                i++;                count++;            }        }        return count;    }};
原创粉丝点击