当前位置:网站首页>455. Distribute cookies [double pointer ++i, ++j]
455. Distribute cookies [double pointer ++i, ++j]
2022-07-26 18:27:00 【LIZHUOLONG1】
455. 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 .
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 <= g.length <= 3 * 104
0 <= s.length <= 3 * 104
1 <= g[i], s[j] <= 231 - 1
Ideas :
Find the biggest appetite that cookies can satisfy g[i] < s[j], When traversing, the condition can only g[i] > s[j] :
Ergodic time , Take the appetite value as the benchmark , Traverse Biscuit size , The pointer j, With g[i] > s[j] Traverse the condition , After traversing the cookie size, it is over .
Java Code :
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int numOfChildren = g.length;
int 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;
}
}
Js Code :
var findContentChildren = function(g, s) {
g.sort((a,b) => a-b); // () Return results
s.sort((a,b) => a-b);
let numOfChild = g.length, numOfCookies = s.length;
let count=0;
for(let i = 0, j = 0; i < numOfChild && j < numOfCookies; i++, j++) {
while(j < numOfCookies && g[i] > s[j]) {
j++; // Traverse j The words are equivalent to g[i] Set the point , scanning s[j]
}
if(j < numOfCookies){
count++;
}
}
return count;
};
边栏推荐
- Understanding service governance in distributed development
- Redis持久化RDB/AOF
- Redis核心原理
- If the recommendation effect is not satisfactory, it's better to try to learn the propeller chart
- VIM multiline operation
- drools-基础语法
- 你适合做自动化 测试吗?
- It is said that the salary of Alibaba P7 is really fragrant
- Sword finger offer regular expression matching
- Arm中国回应“断供华为”事件!任正非表示“没有影响”!
猜你喜欢
随机推荐
贪心——455. 分发饼干
How to assemble a registry
Redis master-slave replication, read-write separation, sentinel mode
Day 4 of SSM practice_ Get user name_ User exit_ User CRUD_ Password encryption_ Roles_ jurisdiction
How about the employment prospects of Russian translation? How to do a good job of Russian translation
[ Kitex 源码解读 ] 服务发现
ssm练习第三天_分页助手_安全框架
Explain in detail the implementation of grpc client long connection mechanism
隐私计算基础组件系列-混淆电路
链表-两个链表的第一个公共结点
成为测试/开发程序员,小张:现实就来了个下马威......
Linked list - reverse linked list
Redis主从复制,读写分离,哨兵模式
The Agile Manifesto has four values and twelve principles
Understanding service governance in distributed development
突发!Arm停止与华为合作!对华为影响究竟有多大?
8.1 Diffie Hellman key exchange
ssm练习第二天_项目拆分moudle_基本增删改查_批量删除_一对一级联查询
Redis persistent rdb/aof
LeetCode_134_加油站








