Known that some children and some candy, each child needs factor g, the size of each candy s,
when a size of the candy s> = a child needs factor g, representing the confectionery meet children; requires the use of the candy, the maximum number of children to meet?
(A child with only one candy satisfied)
, for example: Demand factor group g = [5,10,2,9,15,9]; Confectionery size of the array s = [6,1,20,3,8]; up to meet the three children.
#include<vector>
#include<algorithm>
class Solution
{
public:
Solution(){}
~Solution(){}
int findContentChildren(std::vector<int>& g, std::vector<int>& s)
{
std::sort(g.begin(), g.end());
std::sort(s.begin(), s. end());
unsigned int child = 0;
unsigned int cookie = 0;
while (child<g.size() && cookie<s.size())
{
if (g[child] <= s[cookie])
{
child++;
}
cookie++;
}
return child;
}
};
int main()
{
Solution solve;
std::vector<int> g;
std::vector<int> s;
g.push_back(5);
g.push_back(10);
g.push_back(2);
g.push_back(9);
g.push_back(15);
g.push_back(9);
s.push_back(6);
s.push_back(1);
s.push_back(20);
s.push_back(3);
s.push_back(8);
printf("%d\n",solve.findContentChildren(g,s));
return 0;
}
The result:
3