Pass 1 (left to right): If rating[i] > rating[i-1], give candy[i] = candy[i-1] + 1. Pass 2 (right to left): If rating[i] > rating[i+1] and candy[i] <= candy[i+1], give candy[i] = candy[i+1] + 1.
This handles both directions. Each pass ensures one direction of the constraint. Time: O(n). Space: O(n). Common bug: using only one pass and missing valleys where you need more candies from the right.