Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a2ef34

Browse files
committed
write 455
1 parent 742e36b commit 7a2ef34

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

‎problems/0455.assign-cookies/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
11

22
# 0455.assign-cookies
33

4+
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
5+
6+
对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
7+
8+
9+
示例 1:
10+
11+
输入: g = [1,2,3], s = [1,1]
12+
输出: 1
13+
解释:
14+
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
15+
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
16+
所以你应该输出1。
17+
示例 2:
18+
19+
输入: g = [1,2], s = [1,2,3]
20+
输出: 2
21+
解释:
22+
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
23+
你拥有的饼干数量和尺寸都足以让所有孩子满足。
24+
所以你应该输出2.
25+
26+
27+
提示:
28+
29+
1 <= g.length <= 3 * 104
30+
0 <= s.length <= 3 * 104
31+
1 <= g[i], s[j] <= 231 - 1
32+
33+
```text
34+
来源:力扣(LeetCode)
35+
链接:https://leetcode-cn.com/problems/assign-cookies
36+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
37+
```

‎problems/0455.assign-cookies/run.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
1-
21
package cookies
32

3+
import "fmt"
4+
45
func Run() {
6+
g := []int{1, 2, 3}
7+
s := []int{1, 1}
8+
res := findContentChildren(g, s)
9+
fmt.Println(res)
10+
}
11+
12+
func findContentChildren(g []int, s []int) int {
13+
children := bubbleSort(g)
14+
cookies := bubbleSort(s)
15+
child, cookie := 0, 0
16+
for child < len(children) && cookie < len(cookies) {
17+
if children[child] <= cookies[cookie] {
18+
child++
19+
}
20+
cookie++
21+
}
22+
return child
23+
}
524

25+
func bubbleSort(arr []int) []int {
26+
l := len(arr)
27+
for i := 0; i < l; i++ {
28+
for j := 0; j < l-1-i; j++ {
29+
if arr[j] > arr[j+1] {
30+
arr[j], arr[j+1] = arr[j+1], arr[j]
31+
}
32+
}
33+
}
34+
return arr
635
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /