当前位置:网站首页>[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;
}
边栏推荐
- China as resin Market Research and investment forecast report (2022 Edition)
- PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
- Unity find the coordinates of a point on the circle
- The next key of win generates the timestamp file of the current day
- Database under unity
- 2020-10-27
- Redis has four methods for checking big keys, which are necessary for optimization
- 【Leetcode】1352. Product of the last K numbers
- Basic knowledge points of dictionary
- Research on the value of background repeat of background tiling
猜你喜欢
Download and use of font icons
[trans]: spécification osgi
[转]: OSGI规范 深入浅出
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
[转]MySQL操作实战(一):关键字 & 函数
54. Spiral matrix & 59 Spiral matrix II ●●
Embedded database development programming (zero)
stm32Cubemx(8):RTC和RTC唤醒中断
BUUCTF MISC
支持多模多态 GBase 8c数据库持续创新重磅升级
随机推荐
Web APIs DOM节点
Shell Sort
[turn to] MySQL operation practice (I): Keywords & functions
Out and ref functions of unity
China as resin Market Research and investment forecast report (2022 Edition)
质量体系建设之路的分分合合
Django reports an error when connecting to the database. What is the reason
远程升级怕截胡?详解FOTA安全升级
Solon Logging 插件的添加器级别控制和日志器的级别控制
TF-A中的工具介绍
Unity writes timetables (without UI)
Basic knowledge points of dictionary
3dsmax2018 common operations and some shortcut keys of editable polygons
Unity connects to the database
Embedded database development programming (V) -- DQL
Pause and resume of cocos2dx Lua scenario
cocos2dx_ Lua particle system
Database under unity
嵌入式数据库开发编程(五)——DQL
Unity enables mobile phone vibration