Skip to content

135. Candy #1758

Answered by mah-shamim
mah-shamim asked this question in Q&A
Jun 2, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to distribute candies to children standing in a line such that each child gets at least one candy, and children with higher ratings than their neighbors receive more candies. The goal is to determine the minimum number of candies required to meet these conditions.

Approach

  1. Initialization: Start by assigning each child one candy, as every child must have at least one candy.
  2. Left to Right Pass: Traverse the array from left to right. For each child, if their rating is higher than the left neighbor's rating, assign them one more candy than the left neighbor. This ensures that higher-rated children to the right get more candies than their left neighbors.
  3. Right to Left Pass: Traverse t…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jun 2, 2025
Maintainer Author

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Jun 2, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jun 2, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants