Problem:
Solution:
使用sort()函数先将kids数组与cookies数组进行升序排列,然后在按照greed和size从小到大进行分配。
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
int result = 0;
int i = 0;
int j = 0;
int length_kid = g.size();
int length_cookie = s.size();
sort(g.begin(),g.end());
sort(s.begin(),s.end());
while(j < length_cookie && i < length_kid)
{
while(g[i] > s[j] && j < length_cookie)
{
j++;
}
if(j != length_cookie)
{
j++;
result++;
}
i++;
}
return result;
}
};