FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_04/id_131/LeetCode_455_131.cpp at master · feixiangcode/algorithm · GitHub
feixiangcode
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_04
/
id_131
/
LeetCode_455_131.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (53 loc) · 1.31 KB
Breadcrumbs
algorithm
/
Week_04
/
id_131
/
LeetCode_455_131.cpp
Copy path
File metadata and controls
64 lines (53 loc) · 1.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
455. 分发饼干
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。
但是,每个孩子最多只能给一块饼干。对每个孩子 i ,都有一个胃口值 gi ,
这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j ,都有一个尺寸 sj 。
如果 sj >= gi ,我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。
你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
注意:
你可以假设胃口值为正。
一个小朋友最多只能拥有一块饼干。
示例 1:
输入: [1,2,3], [1,1]
输出: 1
解释:
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。
示例 2:
输入: [1,2], [1,2,3]
输出: 2
解释:
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
你拥有的饼干数量和尺寸都足以让所有孩子满足。
所以你应该输出2.
*/
/*
思路 1 :
将数组从小到大排序
*/
class
Solution
{
public:
int
findContentChildren
(vector<
int
>& g, vector<
int
>& s) {
sort
(g.
begin
(), g.
end
());
sort
(s.
begin
(), s.
end
());
int
res =
0
;
//
满足孩子的数量
int
sj =
0
;
//
记录消费饼干的下标
for
(
int
gi =
0
; gi < g.
size
(); ++gi)
{
while
(sj < s.
size
())
{
if
(s[sj] >= g[gi])
{
++res;
++sj;
break
;
}
++sj;
}
if
(sj == s.
size
())
break
;
}
return
res;
}
};
Back
|
FazBrowse Home
|
New Git URL