Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Candy
Bookmark
Input
Example 1
Example 2
Mountain
Descending
Ascending
Custom
ratings
=
[1, 0, 2]
ratings
candies
1
0
0
1
2
2
ratings
candies
1
0
0
1
2
2
ratings
candies
1
0
0
1
2
2
1
1
1
candies = [
1, 1, 1
]
pass
1 →
ratings
candies
1
0
0
1
2
2
≤
1
1
1
i
r
0
≤
1
→ keep
1
pass
1 →
ratings
candies
1
0
0
1
2
2
>
1
1
2
i
r
2
>
0
→ candies =
1
+ 1 =
2
pass
2 ←
ratings
candies
1
0
0
1
2
2
≤
1
1
2
i
r
0
≤
2
→ keep
1
pass
2 ←
ratings
candies
1
0
0
1
2
2
>
2
1
2
i
r
1
>
0
→ candies = max(
1
,
1
+ 1) =
2
Σ candies =
5
ratings
candies
1
0
0
1
2
2
2
1
2
2 + 1 + 2
=
5
Σ candies =
5
ratings
candies
1
0
0
1
2
2
2
1
2
Minimum candies =
5
algo
master
.
io
Step:
Start: Hand out the fewest candies while respecting the ratings
0 / 7
Input
Example 1
Example 2
Mountain
Descending
Ascending
Custom
ratings
=
[1, 0, 2]
0 / 7
algo
master
.
io
Step:
Start: Hand out the fewest candies while respecting the ratings