mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
docs: 分发饼干添加视频讲解链接
This commit is contained in:
@ -4,31 +4,35 @@
|
||||
</a>
|
||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
|
||||
# 455.分发饼干
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/assign-cookies/)
|
||||
|
||||
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
|
||||
|
||||
对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
|
||||
对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
|
||||
|
||||
示例 1:
|
||||
* 输入: g = [1,2,3], s = [1,1]
|
||||
* 输出: 1
|
||||
解释:你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。所以你应该输出1。
|
||||
示例 1:
|
||||
|
||||
示例 2:
|
||||
* 输入: g = [1,2], s = [1,2,3]
|
||||
* 输出: 2
|
||||
* 解释:你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。你拥有的饼干数量和尺寸都足以让所有孩子满足。所以你应该输出2.
|
||||
- 输入: g = [1,2,3], s = [1,1]
|
||||
- 输出: 1
|
||||
解释:你有三个孩子和两块小饼干,3 个孩子的胃口值分别是:1,2,3。虽然你有两块小饼干,由于他们的尺寸都是 1,你只能让胃口值是 1 的孩子满足。所以你应该输出 1。
|
||||
|
||||
示例 2:
|
||||
|
||||
- 输入: g = [1,2], s = [1,2,3]
|
||||
- 输出: 2
|
||||
- 解释:你有两个孩子和三块小饼干,2 个孩子的胃口值分别是 1,2。你拥有的饼干数量和尺寸都足以让所有孩子满足。所以你应该输出 2.
|
||||
|
||||
提示:
|
||||
* 1 <= g.length <= 3 * 10^4
|
||||
* 0 <= s.length <= 3 * 10^4
|
||||
* 1 <= g[i], s[j] <= 2^31 - 1
|
||||
|
||||
- 1 <= g.length <= 3 \* 10^4
|
||||
- 0 <= s.length <= 3 \* 10^4
|
||||
- 1 <= g[i], s[j] <= 2^31 - 1
|
||||
|
||||
# 视频讲解
|
||||
|
||||
**《代码随想录》算法视频公开课:[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
@ -46,14 +50,12 @@
|
||||
|
||||

|
||||
|
||||
|
||||
这个例子可以看出饼干9只有喂给胃口为7的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。
|
||||
|
||||
这个例子可以看出饼干 9 只有喂给胃口为 7 的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。
|
||||
|
||||
C++代码整体如下:
|
||||
|
||||
```CPP
|
||||
// 版本一
|
||||
// 版本一
|
||||
// 时间复杂度:O(nlogn)
|
||||
// 空间复杂度:O(1)
|
||||
class Solution {
|
||||
@ -63,8 +65,8 @@ public:
|
||||
sort(s.begin(), s.end());
|
||||
int index = s.size() - 1; // 饼干数组的下标
|
||||
int result = 0;
|
||||
for (int i = g.size() - 1; i >= 0; i--) { // 遍历胃口
|
||||
if (index >= 0 && s[index] >= g[i]) { // 遍历饼干
|
||||
for (int i = g.size() - 1; i >= 0; i--) { // 遍历胃口
|
||||
if (index >= 0 && s[index] >= g[i]) { // 遍历饼干
|
||||
result++;
|
||||
index--;
|
||||
}
|
||||
@ -74,27 +76,25 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
从代码中可以看出我用了一个index来控制饼干数组的遍历,遍历饼干并没有再起一个for循环,而是采用自减的方式,这也是常用的技巧。
|
||||
从代码中可以看出我用了一个 index 来控制饼干数组的遍历,遍历饼干并没有再起一个 for 循环,而是采用自减的方式,这也是常用的技巧。
|
||||
|
||||
有的同学看到要遍历两个数组,就想到用两个for循环,那样逻辑其实就复杂了。
|
||||
有的同学看到要遍历两个数组,就想到用两个 for 循环,那样逻辑其实就复杂了。
|
||||
|
||||
### 注意事项
|
||||
|
||||
### 注意事项
|
||||
注意版本一的代码中,可以看出来,是先遍历的胃口,在遍历的饼干,那么可不可以 先遍历 饼干,在遍历胃口呢?
|
||||
|
||||
注意版本一的代码中,可以看出来,是先遍历的胃口,在遍历的饼干,那么可不可以 先遍历 饼干,在遍历胃口呢?
|
||||
其实是不可以的。
|
||||
|
||||
其实是不可以的。
|
||||
外面的 for 是里的下标 i 是固定移动的,而 if 里面的下标 index 是符合条件才移动的。
|
||||
|
||||
外面的for 是里的下标i 是固定移动的,而if里面的下标 index 是符合条件才移动的。
|
||||
|
||||
如果 for 控制的是饼干, if 控制胃口,就是出现如下情况 :
|
||||
如果 for 控制的是饼干, if 控制胃口,就是出现如下情况 :
|
||||
|
||||

|
||||
|
||||
if 里的 index 指向 胃口 10, for里的i指向饼干9,因为 饼干9 满足不了 胃口10,所以 i 持续向前移动,而index 走不到` s[index] >= g[i]` 的逻辑,所以index不会移动,那么当i 持续向前移动,最后所有的饼干都匹配不上。
|
||||
|
||||
所以 一定要for 控制 胃口,里面的if控制饼干。
|
||||
if 里的 index 指向 胃口 10, for 里的 i 指向饼干 9,因为 饼干 9 满足不了 胃口 10,所以 i 持续向前移动,而 index 走不到` s[index] >= g[i]` 的逻辑,所以 index 不会移动,那么当 i 持续向前移动,最后所有的饼干都匹配不上。
|
||||
|
||||
所以 一定要 for 控制 胃口,里面的 if 控制饼干。
|
||||
|
||||
### 其他思路
|
||||
|
||||
@ -117,11 +117,11 @@ public:
|
||||
return index;
|
||||
}
|
||||
};
|
||||
```
|
||||
```
|
||||
|
||||
细心的录友可以发现,这种写法,两个循环的顺序改变了,先遍历的饼干,在遍历的胃口,这是因为遍历顺序变了,我们是从小到大遍历。
|
||||
细心的录友可以发现,这种写法,两个循环的顺序改变了,先遍历的饼干,在遍历的胃口,这是因为遍历顺序变了,我们是从小到大遍历。
|
||||
|
||||
理由在上面 “注意事项”中 已经讲过。
|
||||
理由在上面 “注意事项”中 已经讲过。
|
||||
|
||||
## 总结
|
||||
|
||||
@ -131,8 +131,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
### Java
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
// 思路1:优先考虑饼干,小饼干先喂饱小胃口
|
||||
@ -151,6 +151,7 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
// 思路2:优先考虑胃口,先喂饱大胃口
|
||||
@ -172,6 +173,7 @@ class Solution {
|
||||
```
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
# 思路1:优先考虑小胃口
|
||||
@ -184,6 +186,7 @@ class Solution:
|
||||
res += 1
|
||||
return res
|
||||
```
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
# 思路2:优先考虑大胃口
|
||||
@ -199,6 +202,7 @@ class Solution:
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
```golang
|
||||
//排序后,局部最优
|
||||
func findContentChildren(g []int, s []int) int {
|
||||
@ -218,6 +222,7 @@ func findContentChildren(g []int, s []int) int {
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
```rust
|
||||
pub fn find_content_children(mut children: Vec<i32>, mut cookie: Vec<i32>) -> i32 {
|
||||
children.sort();
|
||||
@ -236,21 +241,21 @@ pub fn find_content_children(mut children: Vec<i32>, mut cookie: Vec<i32>) -> i3
|
||||
```
|
||||
|
||||
### Javascript
|
||||
```js
|
||||
var findContentChildren = function(g, s) {
|
||||
g = g.sort((a, b) => a - b)
|
||||
s = s.sort((a, b) => a - b)
|
||||
let result = 0
|
||||
let index = s.length - 1
|
||||
for(let i = g.length - 1; i >= 0; i--) {
|
||||
if(index >= 0 && s[index] >= g[i]) {
|
||||
result++
|
||||
index--
|
||||
}
|
||||
}
|
||||
return result
|
||||
};
|
||||
|
||||
```js
|
||||
var findContentChildren = function (g, s) {
|
||||
g = g.sort((a, b) => a - b);
|
||||
s = s.sort((a, b) => a - b);
|
||||
let result = 0;
|
||||
let index = s.length - 1;
|
||||
for (let i = g.length - 1; i >= 0; i--) {
|
||||
if (index >= 0 && s[index] >= g[i]) {
|
||||
result++;
|
||||
index--;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
@ -258,41 +263,41 @@ var findContentChildren = function(g, s) {
|
||||
```typescript
|
||||
// 大饼干尽量喂胃口大的
|
||||
function findContentChildren(g: number[], s: number[]): number {
|
||||
g.sort((a, b) => a - b);
|
||||
s.sort((a, b) => a - b);
|
||||
const childLength: number = g.length,
|
||||
cookieLength: number = s.length;
|
||||
let curChild: number = childLength - 1,
|
||||
curCookie: number = cookieLength - 1;
|
||||
let resCount: number = 0;
|
||||
while (curChild >= 0 && curCookie >= 0) {
|
||||
if (g[curChild] <= s[curCookie]) {
|
||||
curCookie--;
|
||||
resCount++;
|
||||
}
|
||||
curChild--;
|
||||
g.sort((a, b) => a - b);
|
||||
s.sort((a, b) => a - b);
|
||||
const childLength: number = g.length,
|
||||
cookieLength: number = s.length;
|
||||
let curChild: number = childLength - 1,
|
||||
curCookie: number = cookieLength - 1;
|
||||
let resCount: number = 0;
|
||||
while (curChild >= 0 && curCookie >= 0) {
|
||||
if (g[curChild] <= s[curCookie]) {
|
||||
curCookie--;
|
||||
resCount++;
|
||||
}
|
||||
return resCount;
|
||||
};
|
||||
curChild--;
|
||||
}
|
||||
return resCount;
|
||||
}
|
||||
```
|
||||
|
||||
```typescript
|
||||
// 小饼干先喂饱小胃口的
|
||||
function findContentChildren(g: number[], s: number[]): number {
|
||||
g.sort((a, b) => a - b);
|
||||
s.sort((a, b) => a - b);
|
||||
const childLength: number = g.length,
|
||||
cookieLength: number = s.length;
|
||||
let curChild: number = 0,
|
||||
curCookie: number = 0;
|
||||
while (curChild < childLength && curCookie < cookieLength) {
|
||||
if (g[curChild] <= s[curCookie]) {
|
||||
curChild++;
|
||||
}
|
||||
curCookie++;
|
||||
g.sort((a, b) => a - b);
|
||||
s.sort((a, b) => a - b);
|
||||
const childLength: number = g.length,
|
||||
cookieLength: number = s.length;
|
||||
let curChild: number = 0,
|
||||
curCookie: number = 0;
|
||||
while (curChild < childLength && curCookie < cookieLength) {
|
||||
if (g[curChild] <= s[curCookie]) {
|
||||
curChild++;
|
||||
}
|
||||
return curChild;
|
||||
};
|
||||
curCookie++;
|
||||
}
|
||||
return curChild;
|
||||
}
|
||||
```
|
||||
|
||||
### C
|
||||
|
Reference in New Issue
Block a user