```
public class Solution {
public int candy(int[] ratings) {
if(ratings == null || ratings.length == 0){
return 0;
}
if (ratings.length < 2) {
return 1;
}
int sum = 0;
int[] list = new int[ratings.length];
for (int i = 0; i < list.length; i++) {
list[i] = 1;
sum += 1;
}
int temp = 0;
list[0] = 1;
for (int i = 0; i < ratings.length; i++) {
temp = i + 1;
if (temp < ratings.length) {
if (ratings[i] < ratings[temp] && list[i] >= list[temp]) {
sum += (list[i] + 1 - list[temp]);
list[temp] = list[i] + 1;
} else if (ratings[i] > ratings[temp] && list[i] <= list[temp]) {
sum = reset(list, ratings, temp, 0, sum);
}
}
}
return sum;
}
public int reset(int[] list, int[] ratings, int high, int low, int sum) {
int temp = high;
for (int i = high; i > low; i--) {
temp = i - 1;
if (temp >= 0) {
if (ratings[i] < ratings[temp] && list[i] >= list[temp]) {
sum += (list[i] + 1 - list[temp]);
list[temp] = list[i] + 1;
} else if (ratings[i] < ratings[temp] && list[i] < list[temp]) {
break;
}
}
}
return sum;
}
```

}