【题目描述】
题目链接:点击这里
【思路分析】
贪心规律:
算法思路:
![](https://img-blog.csdnimg.cn/20190808215522294.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyODE1MTg4,size_16,color_FFFFFF,t_70)
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end()); //孩子的需求因子进行排序
sort(s.begin(),s.end()); //糖果的大小进行排序
int child = 0; //child表示已满足了几个孩子
int cookie = 0; //cookie代表尝试了几个糖果
while(child<g.size()&&cookie<s.size()) {
if(g[child]<=s[cookie]) {
child++; //该糖果满足了孩子,孩子指针child向后移动
}
cookie++; //无论失败与否,每个糖果只尝试一次,cookie向后移动
}
return child; //最终child即为得到满足的孩子的个数
}
};