当前位置:网站首页>[allocation problem] 455 Distribute cookies
[allocation problem] 455 Distribute cookies
2022-07-05 05:15:00 【lee2813】
One 、 subject
Suppose you are a great parent , Want to give your kids some cookies . however , Each child can only give one biscuit at most .
For every child i, All have an appetite value g[i], This is the smallest size of biscuit that can satisfy children's appetite ; And every cookie j, They all come in one size s[j] . If s[j] >= g[i], We can put this biscuit j Assign to children i , The child will be satisfied . Your goal is to meet as many children as possible , And output the maximum value .
Example 1:
Input : g = [1,2,3], s = [1,1]
Output : 1
explain :
You have three children and two biscuits ,3 The appetites of a child are :1,2,3.
Although you have two biscuits , Because they are all of the same size 1, You can only make your appetite worth 1 The children of .
So you should output 1.
Example 2:
Input : g = [1,2], s = [1,2,3]
Output : 2
explain :
You have two children and three biscuits ,2 The appetites of a child are 1,2.
You have enough cookies and sizes to satisfy all children .
So you should output 2.
Two 、 Answer key
The purpose of this problem is that the number of cookies is limited , And to meet more children , The greedy strategy is to give priority to the children with the smallest appetite at each feeding , Only in this way can the final result be greater .
- First , Let's first rank the children according to the degree of hunger .
- then , Start with the first child , That is, the child with the smallest appetite starts to find cookies that can satisfy it , After finding it, start with the next child , Continue searching from the next biscuit , Until all the children have carried out the search operation or the biscuits are exhausted .
3、 ... and 、 Code
int findContentChildren(vector<int> &children,vector<int> &cookies){
sort(children.begin(),children.end());
sort(cookies.begin(),cookies.end());
int child=0;
int cookie=0;
while(cookie<cookies.size()&&child<children.size()){
if(children[child]<=cookies[cookie]) child++;
cookies++;
}
return child;
}
边栏推荐
猜你喜欢
django连接数据库报错,这是什么原因
Recherche de mots pour leetcode (solution rétrospective)
《动手学深度学习》学习笔记
[turn to] MySQL operation practice (I): Keywords & functions
GBase数据库助力湾区数字金融发展
Reverse one-way linked list of interview questions
[转]MySQL操作实战(一):关键字 & 函数
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
Stm32cubemx (8): RTC and RTC wake-up interrupt
2022/7/2 question summary
随机推荐
room数据库的使用
Basic knowledge points of dictionary
Redis 排查大 key 的4种方法,优化必备
Simple HelloWorld color change
GameObject class and transform class of unity
嵌入式数据库开发编程(零)
《动手学深度学习》学习笔记
PMP考生,请查收7月PMP考试注意事项
2022/7/2做题总结
Listview is added and deleted at the index
Pause and resume of cocos2dx Lua scenario
Kali 2018 full image download
2020-10-27
Research on the value of background repeat of background tiling
Chinese notes of unit particle system particle effect
Collapse of adjacent vertical outer margins
小程序直播+电商,想做新零售电商就用它吧!
C # perspective following
Solon 框架如何方便获取每个请求的响应时间?
一个新的微型ORM开源框架