Input: [1,2], [1,2,3] Output: 2 Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. You have 3 cookies and their sizes are big enough to gratify all of the children, You need to output 2.
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
// g小孩需求 s cookies实际数量
sort(s.begin(),s.end());
sort(g.begin(),g.end());
int ans = 0;
int k =0;
for(int i=0;i<g.size();i++)
{
for(int j=k;j<s.size();j++)
{
if(g[i]<=s[j])
{
ans++;
k = j+1;
break;
}
}
}
return ans;
}
};
对vector进行升序排序
sort(g.begin(),g.end());