일부 어린이와 일부 사탕, 각 어린이 요인 g, 각 사탕의 크기, 필요로 알려진
의 사용을 필요로하며 제과 충족 자녀를 대표하는 사탕의> = 아이의 요구 인자 g의 크기, 사탕, 어린이의 최대 수는 충족?
(1 개만 사탕 자식 만족)
예 : 수용 률 그룹 g = [5,10,2,9,15,9] 배열 S = 과자 크기 [6,1,20,3,8]; 위로는 세 자녀를 충족합니다.
#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;
}
결과 :
3