分发饼干

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        //贪心解法,用用尽量小的需求去满足孩子,所以需要先排序
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int num1 = g.size();
        int num2 = s.size();
        int i = 0,j = 0,count = 0;
        while(i!=num1&&j!=num2)//当其中一个遍历结束说明,已经分配最大化
        {
            if(g[i]<=s[j++])//如果当前饼干满足孩子需求,那么孩子数量加一,并且饼干是一直在遍历的,因为如果小的话从i以后的孩子也肯定满足不了所以抛弃了
            {
                i++;
                count++;
            }
        }
        return count;
    }
};

 

posted @ 2021-01-19 16:25  loliconsk  阅读(90)  评论(0)    收藏  举报