# Candy

## Two passes

- from left to right

Ensure any child has more candies than the child on his left

- from right to left

Ensure any child has more candies than the child on his right

### Source code *Read on Github*

```
1 public class Solution {
2 public int candy(int[] ratings) {
3
4
5 int[] candies = new int[ratings.length];
6
7 Arrays.fill(candies, 1);
8
9 for(int i = 1; i < candies.length; i++){
10 if(ratings[i] > ratings[i - 1] && candies[i] <= candies[i - 1])
11 candies[i] = candies[i - 1] + 1;
12 }
13
14 for(int i = candies.length - 2; i >= 0; i--){
15 if(ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1])
16 candies[i] = candies[i + 1] + 1;
17 }
18
19 int s = 0;
20 for(int c : candies)
21 s += c;
22
23 return s;
24
25 }
26 }
```