leet-code/src/main/java/leetcode/editor/cn/Candy.java
2021-09-08 16:50:21 +08:00

68 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。
//
// 你需要按照以下要求,帮助老师给这些孩子分发糖果:
//
//
// 每个孩子至少分配到 1 个糖果。
// 评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。
//
//
// 那么这样下来,老师至少需要准备多少颗糖果呢?
//
//
//
// 示例 1
//
//
//输入:[1,0,2]
//输出5
//解释:你可以分别给这三个孩子分发 2、1、2 颗糖果。
//
//
// 示例 2
//
//
//输入:[1,2,2]
//输出4
//解释:你可以分别给这三个孩子分发 1、2、1 颗糖果。
// 第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
// Related Topics 贪心 数组 👍 645 👎 0
package leetcode.editor.cn;
//135:分发糖果
class Candy {
public static void main(String[] args) {
//测试代码
Solution solution = new Candy().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int candy(int[] ratings) {
int n = ratings.length;
int[] start = new int[n];
for (int i = 0; i < n; i++) {
if (i > 0 && ratings[i] > ratings[i - 1]) {
start[i] = start[i - 1] + 1;
} else {
start[i] = 1;
}
}
int end = 0;
int sum = 0;
for (int i = n - 1; i >= 0; i--) {
if (i < n - 1 && ratings[i] > ratings[i + 1]) {
end++;
} else {
end = 1;
}
sum += Math.max(start[i], end);
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}