I got a wrong answer:
my output is 1+2+3+3+1 = 10. and
I think OJ's is 1+2+3+2+1 = 9
how can the third child has more candies than the fourth?
because the requirement is "Children with a higher rating get more candies than their neighbors", the third child does not have higher rating than the fourth does.
i agree with your case . but they haven't mentioned if two children of the same rating should have same no: of candies . even though this is illogical , i think that's how it works .
in a more comical way :
min(same no:of candies for same rating , lesser no:of candies for same rating)=lesser no: of candies for same rating .
so your algo should accommodate that change .
Remember:"Children with a higher rating get more candies than their neighbors" is the ONLY rule you need to follow. Other than that, any distribution is allowed. When two neighoring kids have the same rating, for example, you can assign any number of candies to them. Both your assignment and the expected assignment are legal, but yours is not optimal.
Well, I think this is ambiguous. "higher rating" than what? Without further specification, it's more reasonable to assume that a child can only get more candies than both neighbors if he has a higher rating than both neighbors. Not higher than one of the neighbors but still getting more candies seems to be a contradiction with the statement in the problem.
But I suppose trying to find solutions to both interpretations is good exercise anyway.
Actually the statement is clear enough from mathematical perspective. You cannot deduce "Children have more candies than their neighbors must have a higher rating" from "Children with a higher rating get more candies than their neighbors". In mathematical words, "Children with a higher rating than their neighbors" is sufficient to "Children get more candies than their neighbors". "Children get more candies than their neighbors" is necessary to "Children with a higher rating than their neighbors", but not sufficient.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.