problem

455. Assign Cookies

solution1:

But, you should give each child at most one cookie. 

对小朋友的满意程度(也就是胃口)和当前cookies的大小分别进行排序,满足一个小朋友则加1;否则比较下一个cookie是否满足小朋友。记住前提是每个小朋友最多只能得到一个cookie.也就是贪婪算法。

class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
int res = ;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
for(int i=, j=; i<g.size() && j<s.size();)//err.
{
if(g[i]<=s[j]) { res++; j++; i++; }
else j++;
}
return res;
}
};

solution2:简化版本,即满足小朋友的个数即是所求解的,小朋友的坐标即是满足小朋友的个数。

class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int i = ;
for(int j=; i<g.size() && j<s.size(); j++)//err.
{
if(g[i]<=s[j]) i++;
}
return i;
}
};

参考

1. Leetcode_455. Assign Cookies;

【leetcode】455. Assign Cookies的更多相关文章

  1. 【LeetCode】455. Assign Cookies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  2. [leetcode greedy]455. Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  3. LeetCode:455. Assign Cookies

    package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...

  4. 455. Assign Cookies - LeetCode

    Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. mac忘记操作密码

    转载于:https://blog.csdn.net/wu110112/article/details/70312987 如果忘记mac登陆密码应该如何处理呢? 这里大家请勿着急,我来帮大家解决这个问题 ...

  2. 【BZOJ4031】小Z的房间

    Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. ...

  3. 使用C#加密及解密字符串

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Util ...

  4. redis 配置初体验

    下载redis 1.新增start.bat 编辑redis-server redis.windows.conf 2..改动redis.windows.conf配置文件改动password:找到例如以下 ...

  5. JS中如何判断对象是对象还是数组

    JS中如何判断对象是对象还是数组 一.总结 一句话总结:typeof Array.isArray === "function",Array.isArray(value)和Objec ...

  6. Kayleigh O'Connor - I Won't Be There

    Do you feel like you're about to drown The wave is rushing over you throw you onto now I remember th ...

  7. Token国内地铁使用城市

    天津 广州 深圳 南京 武汉 台北 高雄

  8. 每天跟着书敲Mysql

    要深入学下Mysql操作啦 CRUD,create,retrieve,update,delete

  9. Crisp String CodeForces - 1117F (状压)

    #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> ...

  10. [hdu P3085] Nightmare Ⅱ

    [hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...