Children stand in a line with ratings. Each child gets at least candy. Children with higher rating than neighbors must get more candies than those neighbors. Minimize total candies.
Example: ratings = [1,0,2]. Child has rating , child has rating , child has rating . Candies: [2,1,2]. Total: .
This needs two passes. One pass cannot satisfy both left and right neighbor constraints simultaneously.