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 9e1a9e9

Browse files
分发饼干
1 parent fdeb397 commit 9e1a9e9

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

‎455.分发饼干.java‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @lc app=leetcode.cn id=455 lang=java
3+
*
4+
* [455] 分发饼干
5+
*
6+
* https://leetcode-cn.com/problems/assign-cookies/description/
7+
*
8+
* algorithms
9+
* Easy (55.74%)
10+
* Likes: 255
11+
* Dislikes: 0
12+
* Total Accepted: 79.4K
13+
* Total Submissions: 138.3K
14+
* Testcase Example: '[1,2,3]\n[1,1]'
15+
*
16+
* 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
17+
*
18+
* 对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >=
19+
* g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
*
25+
* 输入: g = [1,2,3], s = [1,1]
26+
* 输出: 1
27+
* 解释:
28+
* 你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
29+
* 虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
30+
* 所以你应该输出1。
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
*
36+
* 输入: g = [1,2], s = [1,2,3]
37+
* 输出: 2
38+
* 解释:
39+
* 你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
40+
* 你拥有的饼干数量和尺寸都足以让所有孩子满足。
41+
* 所以你应该输出2.
42+
*
43+
*
44+
*
45+
*
46+
* 提示:
47+
*
48+
*
49+
* 1
50+
* 0
51+
* 1
52+
*
53+
*
54+
*/
55+
56+
// @lc code=start
57+
class Solution {
58+
public int findContentChildren(int[] g, int[] s) {
59+
Arrays.sort(g);
60+
Arrays.sort(s);
61+
int j = 0;
62+
int ans = 0;
63+
for (int i = 0; i < g.length; i++) {
64+
while (true) {
65+
if (j >= s.length) {
66+
return ans;
67+
}
68+
if (s[j++] >= g[i]) {
69+
ans++;
70+
break;
71+
}
72+
}
73+
}
74+
return ans;
75+
}
76+
}
77+
// @lc code=end
78+

0 commit comments

Comments
(0)

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