135. Candy

Practice Candy for free on NudgeCode: Hard, Greedy pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Children stand in a line and ratings[i] is the rating of child i. Hand out candy so that every child gets at least one, and any child with a higher rating than a direct neighbour gets more candy than that neighbour. Return the smallest total number of candies needed.

Example

ratings = [1,0,2]  ->  5   (2, 1, 2)
ratings = [1,2,2]  ->  4   (1, 2, 1)

Constraints