要求

  • 贪心算法的关键:判断问题是否可以用贪心算法解决
  • 给小朋友们分饼干,每个小朋友“贪心指数”为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. java学习(更新中)

    class Test { public static void main(String[] args) { System.out.println("Hello World!"); ...

  2. LamPiao靶机work_through

    前言 oscp靶机系列的第二篇.只追求做出来的话,这靶机蛮简单的.但是为了提升难度,尽量避免使用msf--毕竟考试只准用一次嘛,自己写了个exp. 正文 主机发现 nmap -sP 192.168.2 ...

  3. Asp Net Core 5 REST API 使用 RefreshToken 刷新 JWT - Step by Step

    翻译自 Mohamad Lawand 2021年1月25日的文章 <Refresh JWT with Refresh Tokens in Asp Net Core 5 Rest API Step ...

  4. HTML前世今生

    HTML贯穿了整个互联网的发展史,就目前来看,它还会继续下去,因为HTTP和HTTPS协议在互联网中传输的主要内容,也是用户浏览的最终页面管理内容,接下来就来扒一扒HTML的前世今生. 历史舞台已交给 ...

  5. springdata jpa之ddl-auto配置的属性

    在jpa中ddl-auto一共有四种: 分别为: ddl-auto:create ----每次运行该程序,没有表格会新建表格,表内有数据会清空:ddl-auto:create-drop ----每次程 ...

  6. K8s - 解决主机重启后kubelet无法自动启动问题 错误:The connection to the server 192.168.60.128:6443 was refused - did you specify the right host or port?

    1,问题描述 (1)在安装配置好 Kubernetes 后,正常情况下服务器关机重启,kubelet 也会自动启动的.但最近配置的一台服务器重启后,输入命令 kubectl get nodes 查看节 ...

  7. Salesforce学习之路(六)利用Visualforce Page实现页面的动态刷新功能

    Visualforce是一个Web开发框架,允许开发人员构建可以在Lightning平台上本地托管的自定义用户界面.其框架包含:前端的界面设计,使用的类似于HTML的标记语言:以及后端的控制器,使用类 ...

  8. SpringCloud(四)GateWay网关

    GateWay网关 概述简介 Gateway是在 Spring生态系统之上构建的AP网关服务,基于 Spring5, Spring Boot2和 Project Reactor等技术. Gateway ...

  9. Nginx 负载均衡方案

    轮询 根据Nginx配置文件中的顺序,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除. upstream web { server server1; serve ...

  10. 【Springboot】Springboot自动装配原理

    1.核心注解就是 EnableAutoConfiguration  该注解会激活SpringBoot的自动装配功能: 代码如下: @Target(ElementType.TYPE) @Retentio ...