comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
困难 |
|
n
个孩子站成一排。给你一个整数数组 ratings
表示每个孩子的评分。
你需要按照以下要求,给这些孩子分发糖果:
- 每个孩子至少分配到
1
个糖果。 - 相邻两个孩子评分更高的孩子会获得更多的糖果。
请你给每个孩子分发糖果,计算并返回需要准备的 最少糖果数目 。
示例 1:
输入:ratings = [1,0,2] 输出:5 解释:你可以分别给第一个、第二个、第三个孩子分发 2、1、2 颗糖果。
示例 2:
输入:ratings = [1,2,2] 输出:4 解释:你可以分别给第一个、第二个、第三个孩子分发 1、2、1 颗糖果。 第三个孩子只得到 1 颗糖果,这满足题面中的两个条件。
提示:
n == ratings.length
1 <= n <= 2 * 104
0 <= ratings[i] <= 2 * 104
我们初始化两个数组
我们从左到右遍历一遍,如果当前孩子比左边孩子评分高,则
最后,我们遍历一遍评分数组,每个孩子至少应该获得的糖果数为
时间复杂度
class Solution:
def candy(self, ratings: List[int]) -> int:
n = len(ratings)
left = [1] * n
right = [1] * n
for i in range(1, n):
if ratings[i] > ratings[i - 1]:
left[i] = left[i - 1] + 1
for i in range(n - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
right[i] = right[i + 1] + 1
return sum(max(a, b) for a, b in zip(left, right))
class Solution {
public int candy(int[] ratings) {
int n = ratings.length;
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left, 1);
Arrays.fill(right, 1);
for (int i = 1; i < n; ++i) {
if (ratings[i] > ratings[i - 1]) {
left[i] = left[i - 1] + 1;
}
}
for (int i = n - 2; i >= 0; --i) {
if (ratings[i] > ratings[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += Math.max(left[i], right[i]);
}
return ans;
}
}
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> left(n, 1);
vector<int> right(n, 1);
for (int i = 1; i < n; ++i) {
if (ratings[i] > ratings[i - 1]) {
left[i] = left[i - 1] + 1;
}
}
for (int i = n - 2; ~i; --i) {
if (ratings[i] > ratings[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += max(left[i], right[i]);
}
return ans;
}
};
func candy(ratings []int) int {
n := len(ratings)
left := make([]int, n)
right := make([]int, n)
for i := range left {
left[i] = 1
right[i] = 1
}
for i := 1; i < n; i++ {
if ratings[i] > ratings[i-1] {
left[i] = left[i-1] + 1
}
}
for i := n - 2; i >= 0; i-- {
if ratings[i] > ratings[i+1] {
right[i] = right[i+1] + 1
}
}
ans := 0
for i, a := range left {
b := right[i]
ans += max(a, b)
}
return ans
}
function candy(ratings: number[]): number {
const n = ratings.length;
const left = new Array(n).fill(1);
const right = new Array(n).fill(1);
for (let i = 1; i < n; ++i) {
if (ratings[i] > ratings[i - 1]) {
left[i] = left[i - 1] + 1;
}
}
for (let i = n - 2; i >= 0; --i) {
if (ratings[i] > ratings[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
let ans = 0;
for (let i = 0; i < n; ++i) {
ans += Math.max(left[i], right[i]);
}
return ans;
}
public class Solution {
public int Candy(int[] ratings) {
int n = ratings.Length;
int[] left = new int[n];
int[] right = new int[n];
Array.Fill(left, 1);
Array.Fill(right, 1);
for (int i = 1; i < n; ++i) {
if (ratings[i] > ratings[i - 1]) {
left[i] = left[i - 1] + 1;
}
}
for (int i = n - 2; i >= 0; --i) {
if (ratings[i] > ratings[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += Math.Max(left[i], right[i]);
}
return ans;
}
}
class Solution {
public int candy(int[] ratings) {
int n = ratings.length;
int up = 0;
int down = 0;
int peak = 0;
int candies = 1;
for (int i = 1; i < n; i++) {
if (ratings[i - 1] < ratings[i]) {
up++;
peak = up + 1;
down = 0;
candies += peak;
} else if (ratings[i] == ratings[i - 1]) {
peak = 0;
up = 0;
down = 0;
candies++;
} else {
down++;
up = 0;
candies += down + (peak > down ? 0 : 1);
}
}
return candies;
}
}