135. candy

Question

There are N children standing in a line. Each child is assigned a rating value.

You are giving candies to these children subjected to the following requirements:

  • Each child must have at least one candy.
  • Children with a higher rating get more candies than their neighbors.
    What is the minimum candies you must give?

Analysis

基本的,每个孩子最少要拿到一枚糖,
从左往右如果ratings[i] < ratings[i+1] 第i+1th孩子 应该 拿到ith 孩子的糖 + 1
从右往左ratings[i-1] > ratings[i] 并且 i-1th 孩子的糖少于 ith孩子 第i-1th孩子 应该 拿到ith 孩子的糖 + 1

Complexity

Time: O(n)
Space: O(n)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public int (int[] ratings) {
int sum = 0;
int[] a = new int[ratings.length];
for (int i = 0; i < a.length; i++) {
a[i] = 1;
}
for (int i = 0; i < ratings.length - 1; i++) {
if (ratings[i+i] > ratings[i]) {
a[i+1] = a[i] + 1;
}
}

for (int i = ratings.length - 1; i > 0; i--) {
if (ratings[i-1] > ratings[i]) {
if (a[i-1] < (a[i] + 1)) {
a[i-1] = a[i] + 1;
}
}
}
for (int i = 0; i < a.length; i++) {
sum += a[i];
}

return sum;
}