Handing out candy
Children stand in a line, each with a rating. You hand out candy so that every child gets at least one, and any child with a strictly higher rating than an immediate neighbor gets strictly more candy than that neighbor.
ratings = [1, 0, 2] -> 5 (candies 2, 1, 2)
ratings = [1, 2, 2] -> 4 (candies 1, 2, 1)
Return the minimum total candy. Aim for .
Show a hint
Each child faces two constraints at once, one from the left neighbor and one from the right. Try satisfying them in two separate passes and then combining.
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.