Candy
双向爬坡。从左到右爬一边,再从右到左爬一边。再累加所有值即可。
java
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; } }
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。