Greedy Algorithms8 sections · 316 units
Open in Course

Problem - Candy

Distribute fairly

Children stand in a line with ratings. Each child gets at least 11 candy. Children with higher rating than neighbors must get more candies than those neighbors. Minimize total candies.

Example: ratings = [1,0,2]. Child 00 has rating 11, child 11 has rating 00, child 22 has rating 22. Candies: [2,1,2]. Total: 55.

This needs two passes. One pass cannot satisfy both left and right neighbor constraints simultaneously.