- step 1: give each child one candy regardless of his/her rating
- step 2: forward, if i-th child has higher rating than (i-1)-th child, candies[i] should be greater than candies[i-1]
- step 3: backward, if i-th child has higher rating than (i+1)-th child, candies[i] should be greater candies[i+1]
- return sum of candies
Ref
OJ
[2] https://leetcode.com/discuss/23835/one-pass-constant-space-java-solution
One pass, O(1) space
[2] https://leetcode.com/discuss/23835/one-pass-constant-space-java-solution
One pass, O(1) space
No comments:
Post a Comment