题目455. Assign Cookies
等级: easy
标签: 贪心
思路
对g数组和s数组从小到大排序,然后开始从左到右扫描,找到满足当前的s元素大于等于当前g元素时候,个数加一
实现
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
int g_s = g.size(), s_s = s.size();
if(s_s == 0) return 0;
if(g_s == 0) return 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int g_i = 0, s_i = 0;
while(s_i < s_s && g_i < g_s) {
if(s[s_i] >= g[g_i]) {
s_i++;
g_i++;
} else s_i++;
}
return g_i;
}
};