findingsea's Studio.

Candy@LeetCode

Word count: 123 / Reading time: 1 min
2015/04/24 Share

Candy

双向爬坡。从左到右爬一边,再从右到左爬一边。再累加所有值即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
public int candy(int[] ratings) {
int[] candies = new int[ratings.length];
int last = ratings.length - 1;
int result = 0;
for (int i = 1; i <= last; i++) {
if (ratings[i - 1] < ratings[i]) {
candies[i] = candies[i - 1] + 1;
}
}
for (int i = last - 1; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
}
result += candies[i];
}
result += ratings.length + candies[last];
return result;
}
}
CATALOG
  1. 1. Candy