Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

Boss fightThe Candy Slopes

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Satisfy directional inequalities in two passes: The Candy Slopes.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Two-direction greedy constraints matches the clues
  2. 2Keep this true after every move: after each directional pass, all rating inequalities in that direction are minimally satisfied
  3. 3Reach the result within O(n)

Rules and pressure

  • Target cost: O(n)
  • State rule: after each directional pass, all rating inequalities in that direction are minimally satisfied
Lesson 1 of 3

Live algorithm trace

Two-direction greedy constraints

Complete execution
1 of 6

Give every child the required baseline of one candy.

1candies = ones
2scan left to right
3if rating rises: candies[i] = candies[i-1] + 1
4scan right to left
5if rating rises backward: take max with right + 1
6return candy sum
ratings = [1,0,2]candies = [1,1,1]
Truth to preserve / Cost target
Truth to preserve

after each directional pass, all rating inequalities in that direction are minimally satisfied

Cost target

O(n)

A left-to-right pass satisfies every rising edge; a right-to-left pass satisfies every falling edge. Taking the larger requirement at each child meets both local constraints with no excess.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead