要求

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

    1.main.cpp #include "themewidget.h" #include <QtWidgets/QApplication> #include <Q ...

  2. MacBook读写移动硬盘

    在MacBook上插入移动硬盘,只能读取,不能写入.这是因为移动硬盘的格式是NTFS,MacBook不支持写入,有三种方法: 1. 改变移动硬盘的格式,格式化为可以读写的exFAT等格式,但存储的文件 ...

  3. 敏捷史话(十一):敏捷宣言“间谍”——Steve Mellor

    Steve Mellor 是敏捷宣言的签署人之一,他自称是作为" 间谍"去参加雪鸟会议的. 起初收到会议邀请时,Steve 非常惊讶,因为他所做的工作一直都是关于建模方面的,很少将 ...

  4. 01-MySQL Linux安装

    一.检查当前系统是否安装过mysql rpm -qa|grep mysql 或 ps -ef|grep mysql 二.安装mysql服务端 rpm -ivh MySQL-server-5.5.48- ...

  5. 安装Dynamics CRM Report出错处理一

    删除下面两个注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce.HKEY_CURRENT_USER\So ...

  6. python基础(补充):递归的深度

    我们在正经人谁用递归呀一节中,简单的讨论了python中的递归 相信用过python递归的朋友可能都碰到过: RecursionError: maximum recursion depth excee ...

  7. dubbo负载均衡策略和集群容错策略都有哪些?动态代理策略呢?

    (1)dubbo负载均衡策略 1)random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重 ...

  8. [源码分析] 分布式任务队列 Celery 之 发送Task & AMQP

    [源码分析] 分布式任务队列 Celery 之 发送Task & AMQP 目录 [源码分析] 分布式任务队列 Celery 之 发送Task & AMQP 0x00 摘要 0x01 ...

  9. 简单的了解下Java设计模式:迭代器模式(转载)

    迭代器模式定义 迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示. Java 开发过程中遍历是常用的.如下边程序: for(int i =0 ; ...

  10. 【运维】Shell -- 快速上手Shell脚本

    1.Shell概述 shell脚本是利用shell的功能所写的一个[程序(program)].这个程序是使用纯文本文件,将一些shell的语法与命令(含外部命令)写在里面,搭配正则表达式.管道命令与数 ...