当前位置:网站首页>Greedy - 455. Distribute cookies
Greedy - 455. Distribute cookies
2022-07-26 18:11:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- Distribute cookies
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 .
2 Title Example
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.
source : Power button (LeetCode)
link :https://leetcode.cn/problems/assign-cookies
3 Topic tips
1 <= g.length <= 3 * 104
0 <= s.length <= 3 * 104
1 <= g[i], s[j] <= 231 - 1
4 Ideas
In order to meet as many children as possible , From the perspective of greed , Each child should be satisfied in the order of their appetite from small to large , And for every child , Choose the smallest biscuit that meets the child's appetite . Prove the following .
Suppose there is m A child , The appetites are g1 To gm, Yes n A biscuit , The dimensions are s1 To sn, Satisfy gi≤gi+1 and sj≤sj+1, among 1≤i<m,1≤j<n.
Assume before i to 1 After the first child distributed the biscuits , It can meet the requirements of i The smallest biscuit for a child's appetite is the j A biscuit , namely sj Is the rest of the biscuits to meet gi≤sj The minimum value of , The optimal solution is to put the j A biscuit is allocated to the i A child . If not allocated in this way , Consider the following two scenarios :
- If i<m And gi+1≤sj It was also established , Then if j A biscuit is allocated to the i+1 A child , And there are still some biscuits left , Then you can put the second j+1 A biscuit is allocated to the i A child , The result of distribution will not satisfy more children ;
- If j<n, Then if j+1 A biscuit is allocated to the i A child , When gi+1≤sj when , You can put the second j A biscuit is allocated to the i+1 A child , The result of distribution will not satisfy more children ; When gi+1 > 8j when , The first j A biscuit cannot be assigned to any child , So there is one less available biscuit left , Therefore, the result of distribution will not allow more children to be satisfied , Maybe even because there is less — Fewer children are satisfied with a usable cookie .
Based on the above analysis , You can use greedy methods to satisfy as many children as possible .
First, the array g and s Sort , And then traverse from small to large g Every element in , For each element, find a that satisfies that element s The smallest element in . To be specific , Make i yes g The subscript ,j yes s The subscript , At the beginning à and j All for 0, Do the following .
For each element g Same as , The smallest not found or used j bring g[i]≤s[j], be s[j] You can meet g[i]. because g and s It's in order , Therefore, the whole process only needs to compare the array g and s Traverse once each . When the sum of two arrays — At the end of traversal , It means that all the children are assigned to biscuits , Or all the cookies have been distributed or tried to be distributed ( Maybe some cookies can't be distributed to any children ), At this time, the number of children assigned to biscuits is the maximum number that can be met .
Complexity analysis
Time complexity :O(m log m + n log n), among m and n They are arrays g and s The length of . The time complexity of sorting two arrays is o(m log m + n log n), The time complexity of traversing an array is O(m +n), So the total time complexity is O(m log m + n logn).
· Spatial complexity :O(log m + log n), among m and n They are arrays g and s The length of . The space complexity is mainly the extra space cost of sorting .
5 My answer
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int numOfChildren = g.length, numOfCookies = s.length;
int count = 0;
for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
while (j < numOfCookies && g[i] > s[j]) {
j++;
}
if (j < numOfCookies) {
count++;
}
}
return count;
}
}
边栏推荐
- How to switch nodejs versions at will?
- [training day3] section
- [training Day1] spy dispatch
- 【静态代码质量分析工具】上海道宁为您带来SonarSource/SonarQube下载、试用、教程
- 【数字IC】深入浅出理解AXI-Lite协议
- AI zhetianchuan ml unsupervised learning
- 8.1 Diffie Hellman key exchange
- VS Code 格式化后 自动让函数名后有空格
- Mondriaans's dream (state compression DP)
- 【集训Day3】delete
猜你喜欢

BulletGraph(子弹图、项目符号图)

来吧开发者!不只为了 20 万奖金,试试用最好的“积木”来一场头脑风暴吧!

带你熟悉云网络的“电话簿”:DNS

【集训Day1】Spy dispatch

Deep learning experiment: softmax realizes handwritten digit recognition

5、 Parameter server principle, code implementation

Tianyi cloud web application firewall (edge cloud version) supports the detection and interception of Apache spark shell command injection vulnerabilities

Spark data format unsafe row

【云原生】 iVX 低代码开发 引入腾讯地图并在线预览

【集训Day2】Sculpture
随机推荐
2、 Topic communication principle, code implementation
[day3] reconstruction of roads
236. The nearest common ancestor of a binary tree
[Oumi reading club] talk about the creator economy in the meta universe: infinite dimension
kaggle猫狗数据集开源——用于经典CNN分类实战
【欧米读书会】谈谈元宇宙中的创作者经济:无限次元
LeetCode50天刷题计划(Day 1—— 两数相加 11.00-12.30)
Overview of the agenda of the keynote speech of apachecon Asia, an international celebrity vs a local open source star
How to switch nodejs versions at will?
AI zhetianchuan ml unsupervised learning
Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
【翻译】为什么你需要一个API网关来管理对你的API的访问?
DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database
Detailed explanation of openwrt's feeds.conf.default
兆骑科创海外高层次人才引进平台,创业赛事活动路演
2022年的PMP考试大纲是什么?
2022河南萌新联赛第(三)场:河南大学
[training Day2] sculpture
点云目标检测KITTI数据集bin文件可视化,一站式解决
Linux Installation mysql8.0.29 detailed tutorial