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
- 1Recognize when Two-direction greedy constraints matches the clues
- 2Keep this true after every move: after each directional pass, all rating inequalities in that direction are minimally satisfied
- 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 execution1 of 6
Give every child the required baseline of one candy.
1
candies = ones2
scan left to right3
if rating rises: candies[i] = candies[i-1] + 14
scan right to left5
if rating rises backward: take max with right + 16
return candy sumratings = [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?