LeetCode 135 Candy - Problem Statement

The problem

Children stand in a line with ratings. Distribute candies so that: 1.1. Every child gets at least one candy. 2.2. Children with higher ratings than neighbors get more candies than those neighbors.

Minimize total candies.

With ratings = [1,0,2]:

  • Child 1 (rating 0) gets 1 candy.
  • Child 0 (rating 1 > 0) gets 2 candies.
  • Child 2 (rating 2 > 0) gets 2 candies.
  • Total: 55 candies.

With ratings = [1,2,2]:

  • Child 0: 1 candy.
  • Child 1 (2 > 1): 2 candies.
  • Child 2 (2 = 2): 1 candy (not higher).
  • Total: 44 candies.

Constraints: 11 \le ratings.length 2×104\le 2 \times 10^4.