Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Fair Distribution of Cookies
Bookmark
Input
Standard
Small
Simple
Custom
cookies
=
[8, 15, 10, 20, 8]
,
k
=
2
bags
8
15
10
20
8
bags
8
15
10
20
8
child
1
/ 2
mask = 01110
bags
8
15
10
20
8
→ child 1
→ others
best 45
20
child 1 gets
unreachable
others (best)
dp[0][…] unreachable — skip this split
child
1
/ 2
mask = 10111
bags
8
15
10
20
8
→ child 1
→ others
best 41
31
max
child 1 gets
others (best)
child
1
/ 2
mask = 11101
bags
8
15
10
20
8
→ child 1
→ others
best 46
16
max
child 1 gets
others (best)
child
2
/ 2
mask = 00011
bags
8
15
10
20
8
→ child 2
→ others
15
max
child 2 gets
8
others (best)
dp[2][00011] = max(
8
,
15
) =
15
child
2
/ 2
mask = 01110
bags
8
15
10
20
8
→ child 2
→ others
best 30
35
max
child 2 gets
10
others (best)
child
2
/ 2
mask = 10101
bags
8
15
10
20
8
→ child 2
→ others
best 16
18
max
child 2 gets
8
others (best)
dp[2][10101] = max(
8
,
18
) =
18
(keep 16)
child
2
/ 2
mask = 11011
bags
8
15
10
20
8
child
2
/ 2
mask = 11110
bags
8
15
10
20
8
→ child 2
→ others
best 28
33
max
child 2 gets
20
others (best)
dp[2][11110] = max(
20
,
33
) =
33
(keep 28)
bags
8
15
10
20
8
minimum unfairness = 31
algo
master
.
io
Step:
Start: distribute 5 cookie bags among 2 children
0 / 1123
Input
Standard
Small
Simple
Custom
cookies
=
[8, 15, 10, 20, 8]
,
k
=
2
0 / 1123
algo
master
.
io
Step:
Start: distribute 5 cookie bags among 2 children