One test case input is [1,2,2], the expected result is 4. I can not understand it. Should it be 5?
Am I wrong with the test case?

I cannot understand this as well, below is my code, I looked at other solutions here, I think mine would be simpler in logic and implementation:) Pls feel free to comment:
public class Solution { public int candy(int[] ratings) { if (ratings == null  ratings.length == 0) return 0; else if (ratings.length == 1) return 1; int minp = 0; int min = ratings[0]; for (int i = 1; i < ratings.length; i++) { if (ratings[i] < ratings[0]) minp = i; } int count = 1; int prvcnt = 1; int prvrat = ratings[minp]; for (int i = minp + 1; i < ratings.length; i++) { if (ratings[i] > prvrat) { prvcnt++; } else if (ratings[i] < prvrat) { prvcnt; } count += prvcnt; prvrat = ratings[i]; } prvcnt = 1; prvrat = ratings[minp]; for (int i = minp  1; i >= 0; i) { if (ratings[i] > prvrat) { prvcnt++; } else if (ratings[i] < prvrat) { prvcnt; } count += prvcnt; prvrat = ratings[i]; } return count; }
}

Hi @michaelhu, thanks for your posting. I am not sure if you are providing an answer for this question, or you have same problem as this question mentioned. However, you may encourage to do a another post with your idea.