[刷题] 455 Assign Cookies
要求
- 贪心算法的关键:判断问题是否可以用贪心算法解决
- 给小朋友们分饼干,每个小朋友“贪心指数”为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的更多相关文章
- LeetCode:455. Assign Cookies
		package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ... 
- 【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的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ... 
- 455. Assign Cookies 满足欲望 分配饼干
		[抄题]: Assume you are an awesome parent and want to give your children some cookies. But, you should ... 
- 【LeetCode】455. Assign Cookies 解题报告(Java & Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ... 
- 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 (分发曲奇饼干)
		Assume you are an awesome parent and want to give your children some cookies. But, you should give e ... 
- 455. Assign Cookies.md
		Assume you are an awesome parent and want to give your children some cookies. But, you should give e ... 
- [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 ... 
随机推荐
- pwn题命令行解题脚本
			目录 脚本说明 脚本内容 使用 使用示例 参考与引用 脚本说明 这是专门为本地调试与远程答题准备的脚本,依靠命令行参数进行控制. 本脚本支持的功能有: 本地调试 开启tmux调试 设置gdb断点,支持 ... 
- 抗DDOS应急预案实践-生产环境总结-建议必看
			一.首先摸清楚环境与资源 为DDoS应急预案提供支撑 所在的网络环境中,有多少条互联网出口?每一条带宽多少? 每一条互联网出口的运营商是否支持DDoS攻击清洗,我们是否购买,或可以紧急试用?当发生DD ... 
- C#委托的学习了解
			C#的委托(Delegate)类似于C\C++的函数指针.委托是存有对某一个方法引用的一种引用变量类型,引用可在运行时被改变. 委托特别用于实现事件和回调方法.所有的委托都派生自System.Dele ... 
- 附034.Kubernetes_v1.21.0高可用部署架构二
			kubeadm介绍 kubeadm概述 Kubeadm 是一个工具,它提供了 kubeadm init 以及 kubeadm join 这两个命令作为快速创建 kubernetes 集群的最佳实践. ... 
- editorconfig、eslint、prettier三者的区别、介绍及使用
			每次搭建新项目都少不了这些工具,但时间一久就忘记了,下次搭新项目时又要四处查官方文档,因此特此记录,主要内容是对这三个工具的理解,以及具体使用方式 editorconfig 理解 先看官网的定义: E ... 
- Mybatis3源码笔记(七)Plugin
			1.Mybatis3的插件其实主要是用到了责任链和动态代理两种模式相结合而生成的.下面我们看一个例子,在执行所有update操作时,执行一个小小的测试输出. @Intercepts({@Signatu ... 
- Parentheses Balance UVA - 673
			You are given a string consisting of parentheses () and []. A string of this type is said to be corr ... 
- Ananagrams UVA - 156
			Most crossword puzzle fans are used to anagrams - groups of words with the same letters in differe ... 
- Day03_17_数组
			数组 什么是数组? 数组是多个相同类型数据按照一定顺序排列的有序集合,并使用一个名字命名,通过编号的方式,对这些数据进行统一的管理. 数组也是对象,数组中的元素相当于数组对象的成员变量 数组的长度是固 ... 
- shackdow-socks  搭建
			搭建步骤 wget --no-check-certificate https://raw.githubusercontent.com/teddysun/shadow-1-socks_install/m ... 
