mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
docs: 分发饼干添加视频讲解链接
This commit is contained in:
@ -4,7 +4,6 @@
|
|||||||
</a>
|
</a>
|
||||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||||
|
|
||||||
|
|
||||||
# 455.分发饼干
|
# 455.分发饼干
|
||||||
|
|
||||||
[力扣题目链接](https://leetcode.cn/problems/assign-cookies/)
|
[力扣题目链接](https://leetcode.cn/problems/assign-cookies/)
|
||||||
@ -14,21 +13,26 @@
|
|||||||
对每个孩子 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:
|
示例 1:
|
||||||
* 输入: g = [1,2,3], s = [1,1]
|
|
||||||
* 输出: 1
|
- 输入: g = [1,2,3], s = [1,1]
|
||||||
|
- 输出: 1
|
||||||
解释:你有三个孩子和两块小饼干,3 个孩子的胃口值分别是:1,2,3。虽然你有两块小饼干,由于他们的尺寸都是 1,你只能让胃口值是 1 的孩子满足。所以你应该输出 1。
|
解释:你有三个孩子和两块小饼干,3 个孩子的胃口值分别是:1,2,3。虽然你有两块小饼干,由于他们的尺寸都是 1,你只能让胃口值是 1 的孩子满足。所以你应该输出 1。
|
||||||
|
|
||||||
示例 2:
|
示例 2:
|
||||||
* 输入: g = [1,2], s = [1,2,3]
|
|
||||||
* 输出: 2
|
|
||||||
* 解释:你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。你拥有的饼干数量和尺寸都足以让所有孩子满足。所以你应该输出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,10 +50,8 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
这个例子可以看出饼干 9 只有喂给胃口为 7 的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。
|
这个例子可以看出饼干 9 只有喂给胃口为 7 的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。
|
||||||
|
|
||||||
|
|
||||||
C++代码整体如下:
|
C++代码整体如下:
|
||||||
|
|
||||||
```CPP
|
```CPP
|
||||||
@ -78,7 +80,6 @@ public:
|
|||||||
|
|
||||||
有的同学看到要遍历两个数组,就想到用两个 for 循环,那样逻辑其实就复杂了。
|
有的同学看到要遍历两个数组,就想到用两个 for 循环,那样逻辑其实就复杂了。
|
||||||
|
|
||||||
|
|
||||||
### 注意事项
|
### 注意事项
|
||||||
|
|
||||||
注意版本一的代码中,可以看出来,是先遍历的胃口,在遍历的饼干,那么可不可以 先遍历 饼干,在遍历胃口呢?
|
注意版本一的代码中,可以看出来,是先遍历的胃口,在遍历的饼干,那么可不可以 先遍历 饼干,在遍历胃口呢?
|
||||||
@ -95,7 +96,6 @@ if 里的 index 指向 胃口 10, for里的i指向饼干9,因为 饼干9 满
|
|||||||
|
|
||||||
所以 一定要 for 控制 胃口,里面的 if 控制饼干。
|
所以 一定要 for 控制 胃口,里面的 if 控制饼干。
|
||||||
|
|
||||||
|
|
||||||
### 其他思路
|
### 其他思路
|
||||||
|
|
||||||
**也可以换一个思路,小饼干先喂饱小胃口**
|
**也可以换一个思路,小饼干先喂饱小胃口**
|
||||||
@ -131,8 +131,8 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
// 思路1:优先考虑饼干,小饼干先喂饱小胃口
|
// 思路1:优先考虑饼干,小饼干先喂饱小胃口
|
||||||
@ -151,6 +151,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
// 思路2:优先考虑胃口,先喂饱大胃口
|
// 思路2:优先考虑胃口,先喂饱大胃口
|
||||||
@ -172,6 +173,7 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
# 思路1:优先考虑小胃口
|
# 思路1:优先考虑小胃口
|
||||||
@ -184,6 +186,7 @@ class Solution:
|
|||||||
res += 1
|
res += 1
|
||||||
return res
|
return res
|
||||||
```
|
```
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
# 思路2:优先考虑大胃口
|
# 思路2:优先考虑大胃口
|
||||||
@ -199,6 +202,7 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
//排序后,局部最优
|
//排序后,局部最优
|
||||||
func findContentChildren(g []int, s []int) int {
|
func findContentChildren(g []int, s []int) int {
|
||||||
@ -218,6 +222,7 @@ func findContentChildren(g []int, s []int) int {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
pub fn find_content_children(mut children: Vec<i32>, mut cookie: Vec<i32>) -> i32 {
|
pub fn find_content_children(mut children: Vec<i32>, mut cookie: Vec<i32>) -> i32 {
|
||||||
children.sort();
|
children.sort();
|
||||||
@ -236,21 +241,21 @@ pub fn find_content_children(mut children: Vec<i32>, mut cookie: Vec<i32>) -> i3
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var findContentChildren = function (g, s) {
|
var findContentChildren = function (g, s) {
|
||||||
g = g.sort((a, b) => a - b)
|
g = g.sort((a, b) => a - b);
|
||||||
s = s.sort((a, b) => a - b)
|
s = s.sort((a, b) => a - b);
|
||||||
let result = 0
|
let result = 0;
|
||||||
let index = s.length - 1
|
let index = s.length - 1;
|
||||||
for (let i = g.length - 1; i >= 0; i--) {
|
for (let i = g.length - 1; i >= 0; i--) {
|
||||||
if (index >= 0 && s[index] >= g[i]) {
|
if (index >= 0 && s[index] >= g[i]) {
|
||||||
result++
|
result++;
|
||||||
index--
|
index--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### TypeScript
|
### TypeScript
|
||||||
@ -273,7 +278,7 @@ function findContentChildren(g: number[], s: number[]): number {
|
|||||||
curChild--;
|
curChild--;
|
||||||
}
|
}
|
||||||
return resCount;
|
return resCount;
|
||||||
};
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
@ -292,7 +297,7 @@ function findContentChildren(g: number[], s: number[]): number {
|
|||||||
curCookie++;
|
curCookie++;
|
||||||
}
|
}
|
||||||
return curChild;
|
return curChild;
|
||||||
};
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### C
|
### C
|
||||||
|
Reference in New Issue
Block a user