Problems > Fair Candy for Ratings > Editorial

Fair Candy for Ratings — Solution & Editorial

Back to the Problem

Two sweeps: left to right enforce the rising-neighbour rule, then right to left take the max with the falling-neighbour rule. Sum the candies.

Complexity: O(N)

Watch out: A single left-to-right pass misses descents — the right-to-left pass with max() is essential.