2
public int candy(int[] ratings) {4
int[] left = new int[ratings.length];7
// we are checking from left to right that if the element next to our current element has8
// greater rating, if yes then we are increasing their candy9
for (int i = 0; i < ratings.length - 1; i++) {10
if (ratings[i] < ratings[i + 1]) left[i + 1] = left[i] + 1;13
int[] right = new int[ratings.length];14
Arrays.fill(right, 1);16
// we are checking from right to left if the element after than our current element is greater17
// or not , if yes then we are also checking their candies if greater rating has less number of18
// candies then increasing their candy19
for (int i = ratings.length - 2; i >= 0; i--) {20
if (ratings[i + 1] < ratings[i] && right[i] <= right[i + 1]) right[i] = right[i + 1] + 1;23
for (int i = 0; i < right.length; i++) {24
sum += Math.max(right[i], left[i]);