LeetCode题解:Assign Cookies
2017-01-02 14:25
387 查看
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.
Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.
思路:
注意到一个小孩只能拿到最多一个饼干。于是贪婪算法可解。
题解:
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.
Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.
思路:
注意到一个小孩只能拿到最多一个饼干。于是贪婪算法可解。
题解:
int findContentChildren(std::vector<int>& g, std::vector<int>& s) { std::sort(std::begin(g), std::end(g)); std::sort(std::begin(s), std::end(s)); int count(0); size_t sIter(0); for(size_t i = 0; i < g.size(); ++i) { while(sIter < s.size() && g[i] > s[sIter]) ++sIter; if (sIter < s.size() && g[i] <= s[sIter]) { ++sIter; ++count; } } return count; }
相关文章推荐
- activity中的fragment里面的detroy()方法中清理SharedPreferences文件的问题
- TCP SOCKET封装类
- 网站并发低解决
- 使用RequireJS并实现一个自己的模块加载器 (二)
- JSP 内置对象的应用 session application 大侠 仗义相助啊!!!
- 为什么我的自定义simpleadapter填充listview只显示第一行呢
- php新手求救 session 页面跳转,刷新后session值丢失,session_id变换
- 【求看】记录一旦超过一个值就会溢出
- 2016.12.12产品官网
- 有个困扰我很久问题想让前辈们帮帮忙分析!!
- 看了很长时间都找不出为什么运行不成功,求教各位朋友
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(四)Spring AOP中使用log4j实现http请求日志入mongodb
- hibernate 缓存会不会脏数据
- Android Studio 利用GsonFormat插件解析gson格式问题
- java设计模式之组合模式(Composite)
- httpd安全控制
- coreplot 中怎样给固定点设置符号symbol(就是圆圈、五星、雪花那些),它里面是全部设置,我之需要Y值较大的设置,有没有好的方法,谢谢!
- 我使用zxing的jar包,在6.0系统上(Nexus 5x,安卓6.0.1官方系统)扫描时成像是反的?
- Hdfs上传文件dataNode无法启动
- as使用自动导入v7包,一直没使用as创建过项目,哎