Wrong Anser:
Input: [1,2,2]
Output: 5
Expected: 4
Why expected answer is 4?
i think that should be given 1,2,2 candy. 5 in total.
Anyone can explain it to me?
Based on the expected value, and the problem description, it looks like the only requirement of the solution is that if one index has a higher rating, it must have more candy than the neighbors with a lower rating. I see no requirement where neighboring indices with the same rating must have the same amount of candy. In theory, that means that [1,5,5,5,5] would be solved using [1,2,1,1,1]. Under this interpretation, the solution to your test case would be [1,2,1], as index 2 is only neighbored by one index equal, and none less than it. [1,2,1] is, of course, 4 pieces of candy.