Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Candy
Bookmark
Brute Force
Two-Pass
Slope Counting
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
sweep
1
: changed =
false
pass
2 ←
ratings
candies
1
0
0
1
2
2
>
2
1
1
i
r
1
>
0
→ candies = max(
1
,
1
+ 1) =
2
pass
2 ←
ratings
candies
1
0
0
1
2
2
≤
2
1
1
i
r
0
≤
2
→ keep
1
pass
2 ←
ratings
candies
1
0
0
1
2
2
2
1
2
i
no right neighbour → keep
2
ratings
candies
1
0
0
1
2
2
2
1
2
sweep
2
: changed =
false
pass
2 ←
ratings
candies
1
0
0
1
2
2
>
2
1
2
i
r
1
≤
0
→ keep
2
pass
2 ←
ratings
candies
1
0
0
1
2
2
≤
2
1
2
i
r
0
≤
2
→ keep
1
pass
2 ←
ratings
candies
1
0
0
1
2
2
2
1
2
i
no right neighbour → keep
2
Σ 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 / 17
Input
Example 1
Example 2
Mountain
Descending
Ascending
Custom
ratings
=
[1, 0, 2]
0 / 17
algo
master
.
io
Step:
Start: Hand out the fewest candies while respecting the ratings