Two passes
Time O(n) Space O(n)Start with 1 each. Left to right, then right to left with max. Sum.
import java.util.Arrays;
class Solution {
public int candy(int[] ratings) {
int n = ratings.length;
int[] c = new int[n];
Arrays.fill(c, 1);
for (int i = 1; i < n; i++) if (ratings[i] > ratings[i - 1]) c[i] = c[i - 1] + 1;
for (int i = n - 2; i >= 0; i--) if (ratings[i] > ratings[i + 1]) c[i] = Math.max(c[i], c[i + 1] + 1);
int total = 0;
for (int x : c) total += x;
return total;
}
}Verdict: Each pass enforces one side without breaking the other.