Input: [1,2,2] Expected answer is 4?

  • 5

    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?

  • 2

    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.

  • 0

    Ugh, when I saw "neighbor" I did not interpret it as a neighboring index, but rather as "other children who live in the neighborhood." Unfortunate wording.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.