要求

  • 贪心算法的关键:判断问题是否可以用贪心算法解决
  • 给小朋友们分饼干,每个小朋友“贪心指数”为g(i),饼干大小值s(i)
  • g(i):小朋友需要的饼干大小的最小值
  • 若s(j)>=g(i),则将饼干j分给小朋友i,小朋友会开心
  • 给定s、g,问如何分配饼干,能让最多的小朋友开心

示例

  • g=[1,2,3], s=[1,1], 输出1
  • g=[1,2], s=[1,2,3], 输出2

思路

  • 将最大的饼干给最贪心的小朋友
  • 先对数组排序
  • 复杂度:nlogn

实现

  • 从大到小排序
  • si:指向饼干
  • gi:指向小朋友
  • res:记录有多少小朋友开心

 1 class Solution {
2 public:
3 int findContentChildren(vector<int>& g, vector<int>& s) {
4
5 sort(g.begin(), g.end(), greater<int>());
6 sort(s.begin(), s.end(), greater<int>());
7
8 int si = 0 , gi = 0 ;
9 int res = 0 ;
10 while( gi < g.size() && si < s.size() ){
11
12 if( s[si] >= g[gi] ){
13 res ++ ;
14 si ++ ;
15 gi ++ ;
16 }
17 else
18 gi ++;
19 }
20 return res;
21 }
22 };

相关

  • 392 Is Subsequence

[刷题] 455 Assign Cookies的更多相关文章

  1. LeetCode:455. Assign Cookies

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

  2. 【leetcode】455. Assign Cookies

    problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...

  3. 455. Assign Cookies - LeetCode

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

  4. 455. Assign Cookies 满足欲望 分配饼干

    [抄题]: Assume you are an awesome parent and want to give your children some cookies. But, you should ...

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

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

  6. 12. leetcode 455.Assign Cookies

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

  7. LeetCode 455. Assign Cookies (分发曲奇饼干)

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

  8. 455. Assign Cookies.md

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

  9. [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 ...

随机推荐

  1. 云原生 API 网关,gRPC-Gateway V2 初探

    gRPC-Gateway 简介 我们都知道 gRPC 并不是万能的工具. 在某些情况下,我们仍然想提供传统的 HTTP/JSON API.原因可能从保持向后兼容性到支持编程语言或 gRPC 无法很好地 ...

  2. ls:未找到命令

    解决,别问为什么. 执行 export PATH=/bin:/usr/bin:$PATH

  3. DSP代码搬运至RAM运行

    程序运行过程中,有些函数或程序段和数据等经常调用,正常情况下在FLASH中运行处理消耗时间和资源较大,通常将其移植至RAM中运行,可提高运行效率. 如: 1 #pragma CODE_SECTION( ...

  4. OO第二章总结

    OO第二章总结 电梯作业终于结束了!!! 这三周作业用多线程模拟搭建电梯的运行,我从开始对多线程的一无所知到结束时的能够完成一些多线程任务的水平,进步还是蛮大的,尽管过程有点艰难. 一.复杂度与UML ...

  5. idea无法引入自己定义的包和类

    方法一:通过清理缓存解决: File -> Invalidate Caches / Restart...,在新窗口点击Invalidte and Restart,未奏效 方法二:导入依赖 如图, ...

  6. inline&register

    inline关键字: 内联只是一个请求,不代表编译器会响应:同时某些编译器会将一些函数优化成为内联函数. C++在类内定义的函数默认是内联函数,具体是否真变成内联函数还需看编译器本身. registe ...

  7. 6. Mybatis resultMap

    resultMap 元素是MyBatis中最重要最强大的元素.它就是让你远离90%的需要从结果集中取出数据的JDBC代码的那东西,而且在一些情形下允许你做一些JDBC不支持的事情.事实上,编写相似于对 ...

  8. python set 一些用法

    add(增加元素) name = set(['Tom','Lucy','Ben']) name.add('Juny') print(name)#输出:{'Lucy', 'Juny', 'Ben', ' ...

  9. PowerBI开发 第十八篇:行级安全(RLS)

    PowerBI可以通过RLS(Row-level security)限制用户对数据的访问,过滤器在行级别限制数据的访问,用户可以在角色中定义过滤器,通过角色来限制数据的访问.在PowerBI Serv ...

  10. Kafka2.8安装

    1.概述 最近Kafka官网发布了2.8版本,在该版本中引入了KRaft模式.鉴于新版本和新特性的引入,相关使用资料较少,那边本篇博客笔者将为大家介绍Kafka2.8的安装和使用. 2.内容 2.1  ...