455. 分发饼干

输入: [1,2,3], [1,1]

输出: 1

解释:
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。

尽量让胃口小的孩子分到尽量少的饼干。

先排序。在饼干的循环中,如果孩子的胃口大于等于饼干,孩子加一。

###Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
//先将两组数据排序。
std::sort(g.begin(),g.end());
std::sort(s.begin(),s.end());
//记录被满足的孩子和饼干
int child = 0;
int cookie = 0;
while(cookie < s.size() && child < g.size()){
//饼干可以满足该孩子。
if(g[child] <= s[cookie]){
child++;
}
cookie++;
}
return child;
}
};