LeetCode: 455 Assign Cookies(easy)
题目:
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.
Example 1:
- Input: [1,2,3], [1,1]
- Output: 1
- Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
- And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
- You need to output 1.
Example 2:
- Input: [1,2], [1,2,3]
- Output: 2
- Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
- You have 3 cookies and their sizes are big enough to gratify all of the children,
- You need to output 2.
代码:
- class Solution {
- public:
- int findContentChildren(vector<int>& g, vector<int>& s) {
- int result = ;
- while ((!g.empty())&&(!s.empty())){
- auto gmax = max_element(g.begin(), g.end());
- auto smax = max_element(s.begin(), s.end());
- if (*gmax <= *smax){
- result++;
- g.erase(gmax);
- s.erase(smax);
- }
- else
- g.erase(gmax);
- }
- return result;
- }
- };
改良版:
- class Solution {
- public:
- int findContentChildren(vector<int>& g, vector<int>& s) {
- int result = ;
- sort(g.begin(), g.end(), greater<int>());
- sort(s.begin(), s.end(), greater<int>());
- vector<int>::iterator gtem = g.begin();
- vector<int>::iterator stem = s.begin();
- while (gtem != g.end() && stem != s.end()){
- if (*gtem > *stem)
- gtem++;
- else{
- result++;
- gtem++;
- stem++;
- }
- }
- return result;
- }
- };
LeetCode: 455 Assign Cookies(easy)的更多相关文章
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- LeetCode 455. Assign Cookies (分发曲奇饼干)
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- 12. leetcode 455.Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- LeetCode 455. Assign Cookies (C++)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
- 【leetcode】455. Assign Cookies
problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
- 【LeetCode】455. Assign Cookies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- [LeetCode&Python] Problem 455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- [leetcode greedy]455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
随机推荐
- Hadoop切换namenode为active
hadoop切换namenode为active 进入hadoop/bin目录下 ./yarn rmadmin -transitionToActive --forcemanual rm1 重新启动zkf ...
- 大话设计模式--策略模式 strategy -- C++实现实例
1. 策略模式: 它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户. 用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合. 实例中策 ...
- OTSU大津法对图像二值化
OTSU算法 (1)原理: 对于图像I(x,y),前景(即目标)和背景的分割阈值记作T,属于背景的像素个数占整幅图像的比例记为ω0,其平均灰度μ0:前景像素个数占整幅图像的比例为ω1,其平均灰度为μ1 ...
- python3与Redis连接操作
Python3之redis使用 简介 redis是一个key-value存储系统,和Memcache类似,它支持存储的value类型相对更多,包括string(字符串),list(链表),set( ...
- Web中常用字体介绍
1.在Web编码中,CSS默认应用的Web字体是有限的,虽然在新版本的CSS3,我们可以通过新增的@font-face属性来引入特殊的浏览器加载字体. 浏览器中展示网页文字内容时,文字字体都会按照设计 ...
- HashMap去重
package util; import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import jav ...
- hdu--2570--迷瘴(贪心)
#include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...
- codeforces 707C C. Pythagorean Triples(数学)
题目链接: C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input ...
- 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- ONVIF协议测试工具 ONVIF Device Test Tool 29 12.12 最新版
ONVIF协议测试工具 ONVIF Device Test Tool 29 12.12 最新版 包含文档和工具,本人亲测,好用! http://download.csdn.net/detail/li_ ...