Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Candy

Hard

Topics
ArrayGreedy

There are n children with ratings. Each child gets at least one candy, and a child with a higher rating than an adjacent child must get more candies. Return the minimum total candies.

Example 1

Input:  ratings = [1,0,2]
Output: 5

Example 2

Input:  ratings = [1,2,2]
Output: 4

Constraints

  • n == ratings.length
  • 1 <= n <= 2*10^4
  • 0 <= ratings[i] <= 2*10^4
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access