当前位置:网站首页>Leetcode (455) - distribute cookies
Leetcode (455) - distribute cookies
2022-06-24 20:33:00 【SmileGuy17】
Leetcode(455)—— Distribute cookies
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.
Tips :
- 1 1 1 <= g.length <= 3 ∗ 1 0 4 3 * 10^4 3∗104
- 0 0 0 <= s.length <= 3 ∗ 1 0 4 3 * 10^4 3∗104
- 1 1 1 <= g[i], s[j] <= 2 31 − 1 2^{31 - 1} 231−1
Answer key
Key points : Think about the local optimum , Figure out the global optimum , It feels that the local optimum can deduce the global optimum , And can't think of a counterexample , Then try greed
Method 1 : Sort + greedy
Ideas
Because children with the least hunger are the most likely to be full , So let's think about this kid first . In order to try to make the rest of the biscuits satisfy the hungry children , So we should put more than or equal to this child's hunger degree 、 And the smallest biscuit for the child . After satisfying the child , We take the same strategy , Consider the least hungry child left , Until there are no biscuits that meet the conditions .
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 m m A child , The appetites are g 1 g_1 g1 To g m g_m gm, Yes n n n A biscuit , The dimensions are s 1 s_1 s1 To s n s_n sn, Satisfy g i ≤ g i + 1 g_i \le g_{i+1} gi≤gi+1 and s j ≤ s j + 1 s_j \le s_{j+1} sj≤sj+1, among 1 ≤ i < m 1 \le i < m 1≤i<m, 1 ≤ j < n 1 \le j < n 1≤j<n.
Assume before i − 1 i-1 i−1 After the first child distributed the biscuits , It can meet the requirements of i i i The smallest biscuit for a child's appetite is the j j j A biscuit , namely s j s_j sj Is the rest of the biscuits to meet g i ≤ s j g_i \le s_j gi≤sj The minimum value of , The optimal solution is to put the j j j A biscuit is allocated to the i i i A child . If not allocated in this way , Consider the following two scenarios :
- If i < m i<m i<m And g i + 1 ≤ s j g_{i+1} \le s_j gi+1≤sj It was also established , Then if j j j A biscuit is allocated to the i + 1 i+1 i+1 A child , And there are still some biscuits left , Then you can put the second j + 1 j+1 j+1 A biscuit is allocated to the i i i A child , The result of distribution will not satisfy more children ;
- If j < n j<n j<n, Then if j + 1 j+1 j+1 A biscuit is allocated to the i i i A child , When g i + 1 ≤ s j g_{i+1} \le s_j gi+1≤sj when , You can put the second j j j A biscuit is allocated to the i + 1 i+1 i+1 A child , The result of distribution will not satisfy more children ; When g i + 1 > s j g_{i+1}>s_j gi+1>sj when , The first j j 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 , Even fewer children may be satisfied because of the lack of a usable biscuit .
Based on the above analysis , You can use greedy methods to satisfy as many children as possible .
First, the array g g g and s s s Sort , And then traverse from small to large g g g Every element in , For each element, find a that satisfies that element s s s The smallest element in . To be specific , Make i i i yes g g g The subscript , j j j yes s s s The subscript , At the beginning i i i and j j j All for 0 0 0, Do the following .
For each element g [ i ] g[i] g[i], find Unused The smallest j j j bring g [ i ] ≤ s [ j ] g[i] \le s[j] g[i]≤s[j], be s [ j ] s[j] s[j] You can meet g [ i ] g[i] g[i]. because g g g and s s s It's in order , Therefore, the whole process only needs to compare the array g g g and s s s Traverse once each . When one of the two arrays ends , 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 .
Code implementation
My own :
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
// Ascending sort
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int n = 0;
vector<int>::iterator p = s.begin();
for(auto kid: g){
while(p != s.end()){
// Judge whether this biscuit can make the child full
if(kid <= *p){
n++;
p++;
break;
}else p++;
}
if(p == s.end()) break;
}
return n;
}
};
Complexity analysis
Time complexity : O ( m l o g m + n l o g n ) O(mlogm+nlogn) O(mlogm+nlogn), among m m m and n n n They are arrays g g g and s s s The length of . The time complexity of sorting two arrays is O ( m log m + n log n ) O(m \log m + n \log n) O(mlogm+nlogn), The time complexity of traversing an array is O ( m + n ) O(m+n) O(m+n), So the total time complexity is O ( m log m + n log n ) O(m \log m + n \log n) O(mlogm+nlogn).
Spatial complexity : O ( log m + log n ) O(\log m + \log n) O(logm+logn), among m m m and n n n They are arrays g g g and s s s The length of . The space complexity is mainly the extra space cost of sorting .
边栏推荐
- [cloud resident co creation] ModelBox draws your own painting across the air
- Where is 5g really powerful? What is the difference with 4G?
- 云计算发展的 4 个阶段,终于有人讲明白了
- The AI for emotion recognition was "harbouring evil intentions", and Microsoft decided to block it!
- C語言實現掃雷(簡易版)
- Introduction: continuously update the self-study version of the learning manual for junior test development engineers
- Some ideas about chaos Engineering
- lol手游之任务进度条精准计算
- Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
- Design of routing service for multi Activity Architecture Design
猜你喜欢
Bytebase 加入阿裏雲 PolarDB 開源數據庫社區
Design of routing service for multi Activity Architecture Design
Camera rental management system based on qt+mysql
传统的IO存在什么问题?为什么引入零拷贝的?
Where are Xiaomi mobile phone's favorite SMS and how to delete them
Basic operation of sequence table
Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable
得物多活架构设计之路由服务设计
基于SSM的物料管理系统(源码+文档+数据库)
"Ningwang" was sold and bought at the same time, and Hillhouse capital has cashed in billions by "selling high and absorbing low"
随机推荐
年轻人捧红的做饭生意经:博主忙卖课带货,机构月入百万
Bytebase加入阿里云PolarDB开源数据库社区
图像PANR
First understand redis' data structure - string
顺序栈遍历二叉树
[cann document express issue 06] first knowledge of tbe DSL operator development
It is said that Tencent officially announced the establishment of "XR" department to bet on yuanuniverse; Former CEO of Google: the United States is about to lose the chip competition. We should let T
The Network Security Review Office launched a network security review on HowNet, saying that it "has a large amount of important data and sensitive information"
大一女生废话编程爆火!懂不懂编程的看完都拴Q了
What is showcase? What should showcase pay attention to?
maptalks:数据归一化处理与分层设色图层加载
Fundamentals of performance testing -- definitions of common terms
伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI
Where are Xiaomi mobile phone's favorite SMS and how to delete them
Predicate
Vxlan and MPLS: from data center to Metro Ethernet
用手机摄像头就能捕捉指纹?!准确度堪比签字画押,专家:你们在加剧歧视
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
《梦华录》“超点”,鹅被骂冤吗?
VMware virtual machine setting static IP