Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Assign Cookies
Bookmark
Brute Force
Two Pointers
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
content =
0
g
greed
s
cookies
0
1
2
0
1
1
2
3
1
1
used = [
false
] × 2, content =
0
content =
0
g
greed
s
cookies
0
1
2
0
1
≥?
1
2
3
1
1
i
j
s[0]
1
≥?
g[0]
1
content =
1
g
greed
s
cookies
0
1
2
0
1
≥
1
2
3
1
1
i
j
1
≥
1
→ child satisfied, content =
1
content =
1
g
greed
s
cookies
0
1
2
0
1
≥?
1
2
3
1
1
i
j
s[0]
1
≥?
g[1]
2
content =
1
g
greed
s
cookies
0
1
2
0
1
≥?
1
2
3
1
1
i
j
s[1]
1
≥?
g[1]
2
content =
1
g
greed
s
cookies
0
1
2
0
1
1
2
3
1
1
i
j
g[2]
3
→ scan for the first unused cookie that fits
content =
1
g
greed
s
cookies
0
1
2
0
1
≥?
1
2
3
1
1
i
j
s[0]
1
≥?
g[2]
3
content =
1
g
greed
s
cookies
0
1
2
0
1
≥?
1
2
3
1
1
i
j
s[1]
1
≥?
g[2]
3
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 / 16
Input
Example 1
Example 2
Unsorted input
All satisfied
None satisfied
Mixed
Custom
g
=
[1, 2, 3]
,
s
=
[1, 1]
0 / 16
algo
master
.
io
Step:
Start: Assign cookies to maximize the number of content children