Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Assign Cookies
Bookmark
Input
Example 1
Example 2
Unsorted input
All satisfied
None satisfied
Mixed
Custom
g
=
[1, 2, 3]
,
s
=
[1, 1]
g
greed
s
cookies
0
1
2
0
1
1
2
3
1
1
g
greed
s
cookies
0
1
2
0
1
1
2
3
1
1
g
greed
s
cookies
0
1
2
0
1
1
2
3
1
1
sort
g
and
s
in ascending order
content =
0
g
greed
s
cookies
0
1
2
0
1
1
2
3
1
1
childIndex
cookieIndex
childIndex =
0
, cookieIndex =
0
content =
0
g
greed
s
cookies
0
1
2
0
1
≥?
1
2
3
1
1
childIndex
cookieIndex
s[0]
1
≥?
g[0]
1
content =
1
g
greed
s
cookies
0
1
2
0
1
≥
1
2
3
1
1
childIndex
cookieIndex
1
≥
1
→ child satisfied, content =
1
content =
1
g
greed
s
cookies
0
1
2
0
1
≥?
1
2
3
1
1
childIndex
cookieIndex
s[1]
1
≥?
g[1]
2
content =
1
g
greed
s
cookies
0
1
2
0
1
<
1
2
3
1
1
childIndex
1
<
2
→ cookie too small, skip it
content =
1
g
greed
s
cookies
0
1
2
0
1
1
2
3
1
1
Content children =
1
algo
master
.
io
Step:
Start: Assign cookies to maximize the number of content children
0 / 7
Input
Example 1
Example 2
Unsorted input
All satisfied
None satisfied
Mixed
Custom
g
=
[1, 2, 3]
,
s
=
[1, 1]
0 / 7
algo
master
.
io
Step:
Start: Assign cookies to maximize the number of content children