Assign Cookies
来源:互联网 发布:军娘捏脸数据 编辑:程序博客网 时间:2024/05/16 23:02
题目:
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
解析:
现将给出的两个数组(孩子满意度数组和糖果大小数组)进行排序,排好序后,设置两个点分别存在两个数组中(点作为数组特殊下标),当两个点分别小于两个数组的大小时,进行判定,若孩子满意度数组中的点对应的满意大小值比糖果大小数组中的点对应的大小值要更小,说明满足标准,两个点加加(即跳过这一对),若大于,则只将糖果大小数组的点加加,将下一个糖果大小值与原本不满足的孩子满意度大小值对比,直到有一个点超过其对应数组的大小值,则退出,输出孩子满意度大小值对应的点(即符合条件的点)。
代码:
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int pg=0;
int ps=0;
while(pg<g.length() &&ps<s.length()){
if(g[pg]<=s[ps]){
ps++;
pg++;
}
else{
ps++;
}
}
return pg;
}
};
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- Assign Cookies
- LeetCode 455. Assign Cookies
- 455. Assign Cookies
- Leetcode-455. Assign Cookies
- 455. Assign Cookies
- 455. Assign Cookies
- part-11输入阻抗和输入电容
- 基于python的logistic回归分类
- VS2015错误(二) msvcp120d.dll和msvcr120d.dll缺失
- 第八届蓝桥杯最大公共子串
- Ubuntu下安装Eclipse完整教程
- Assign Cookies
- part-12 输入电容Cin的测量
- vsftpd与PAM模块编译、安装及测试流程
- 给定一个数组,求出数组元素的排列和组合——Java实现
- part-13 轨至轨输入(Rail to Rail)
- 死锁
- 三维扫描仪[11]——总结并展望基于Kinect的三维扫描仪
- 简单的文件上传(javaweb实现)
- Eclipse中安装插件