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