Search⌘ K
AI Features

Solution: Candy

Explore how to apply a two-pass greedy algorithm to distribute candies based on children's ratings. Learn to ensure fairness by giving more candies to children with higher ratings than their neighbors while minimizing the total candies distributed. This lesson helps you implement and understand an efficient O(n) time and space solution to common greedy optimization problems.

Statement

You are given an array ratings where ratings[i] represents the rating of the i-th child standing in a line. Your task is to distribute candies to each child based on the following rules:

  1. Every child must receive at least one candy.

  2. Children with a higher rating get more candies ...