FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_04/id_118/leetcode_455_118.java 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_118
/
leetcode_455_118.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (28 loc) · 992 Bytes
Breadcrumbs
algorithm
/
Week_04
/
id_118
/
leetcode_455_118.java
Copy path
File metadata and controls
33 lines (28 loc) · 992 Bytes
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
// https://leetcode-cn.com/problems/assign-cookies/
// 455.分发饼干
// 解法:每次选一个胃口最小的孩子,给尺寸最小的饼干
class
Solution
{
public
int
findContentChildren
(
int
[]
g
,
int
[]
s
) {
// 分别排序
Arrays
.
sort
(
g
);
Arrays
.
sort
(
s
);
int
i
=
0
;
int
lastMatchIndex
= -
1
;
// 对每一个孩子 i ,从 s 里找第一个比它大的 j,能满足 s[j]>=g[i](尺寸>=胃口)。
// 若某个孩子 i 找不到比它大的 j,则终止,后面的孩子也一定得不到满足
for
(
i
=
0
;
i
<
g
.
length
;
i
++){
boolean
found
=
false
;
for
(
int
j
=
lastMatchIndex
+
1
;
j
<
s
.
length
;
j
++){
if
(
s
[
j
]>=
g
[
i
]){
lastMatchIndex
=
j
;
found
=
true
;
break
;
}
}
if
(
found
==
false
){
break
;
}
}
return
i
;
}
}
Back
|
FazBrowse Home
|
New Git URL