I'm Prime

十方三世,尽在一念

View on GitHub

leetcode 笔记


题目描述

老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。

你需要按照以下要求,帮助老师给这些孩子分发糖果:

示例 1:

输入: [1,0,2]
输出: 5
解释: 你可以分别给这三个孩子分发 212 颗糖果。
示例 2:

输入: [1,2,2]
输出: 4
解释: 你可以分别给这三个孩子分发 121 颗糖果。
     第三个孩子只得到 1 颗糖果,这已满足上述两个条件。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/candy
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

code

开一个等长的数组 cnt,记录每人一颗糖后 i 位置额外要发几颗糖。

从头到尾扫一遍数组,如果 ratings[i] > ratings[i - 1] 说明比前一个人再多发一个糖,cnt[i] = cnt[i - 1] + 1; 否则不用多发任何糖, cnt[i] = 0

同理,从尾往头扫,如果 ratings[i] > ratings[i + 1] 说明要比后一个人再多发一颗糖,需要的糖果数与现有的cnt[i]比较,留下最大的。

结果 人数 + 额外需要糖果数

class Solution
{
public:
    int candy(vector<int> &ratings)
    {
        if (ratings.empty())
            return 0;
        int size = ratings.size();

        vector<int> cnt(size, 0);
        for (int i = 1; i < size; i++)
            if (ratings[i] > ratings[i - 1])
                cnt[i] = cnt[i - 1] + 1;

        int ans = size + cnt[size - 1];
        for (int i = size - 2, pre = 0; i >= 0; i--)
            ans += max(pre = ratings[i] > ratings[i + 1] ? pre + 1 : 0, cnt[i]);

        return ans;
    }
};

一刀流

fun candy(ratings: IntArray): Int = if (ratings.isEmpty()) 0 else ratings.foldIndexed(IntArray(ratings.size)) { i, arr, v -> arr.also { if (i > 0 && v > ratings[i - 1]) arr[i] = arr[i - 1] + 1 } }.let { cnt -> ratings.foldRightIndexed(ratings.size + cnt.last() to 0) { index, v, (ans, pre) -> if (index < ratings.size - 1 && v > ratings[index + 1]) ans + max(cnt[index], pre + 1) to pre + 1 else ans + cnt[index] to 0 } }.first
import kotlin.math.max

class Solution {
    fun candy(ratings: IntArray): Int = if (ratings.isEmpty()) 0 else
        ratings.foldIndexed(IntArray(ratings.size)) { i, arr, v ->
            arr.also { if (i > 0 && v > ratings[i - 1]) arr[i] = arr[i - 1] + 1 }
        }.let { cnt ->
            ratings.foldRightIndexed(ratings.size to 0) { index, v, (ans, pre) ->
                if (index < ratings.size - 1 && v > ratings[index + 1])
                    ans + max(cnt[index], pre + 1) to pre + 1
                else
                    ans + cnt[index] to 0
            }
        }.first
}

一刀流

def candy(ratings: Array[Int]): Int = ratings.indices.foldLeft(Array.fill(ratings.length)(0)) { (arr, i) => if (i > 0 && ratings(i) > ratings(i - 1)) arr(i) = arr(i - 1) + 1; arr }.zipWithIndex.foldRight((ratings.length, 0)) { (i, acc) => if (i._2 < ratings.length - 1 && ratings(i._2) > ratings(i._2 + 1)) (acc._1 + math.max(i._1, acc._2 + 1), acc._2 + 1) else (acc._1 + i._1, 0) }._1
object Solution {
  def candy(ratings: Array[Int]): Int = ratings.indices.foldLeft(Array.fill(ratings.length)(0)) { (arr, i) =>
    if (i > 0 && ratings(i) > ratings(i - 1)) arr(i) = arr(i - 1) + 1; arr
  }.zipWithIndex.foldRight((ratings.length, 0)) { (i, acc) =>
    if (i._2 < ratings.length - 1 && ratings(i._2) > ratings(i._2 + 1))
      (acc._1 + math.max(i._1, acc._2 + 1), acc._2 + 1)
    else
      (acc._1 + i._1, 0)
  }._1
}

一刀流

pub fn candy(ratings: Vec<i32>) -> i32 { (0..ratings.len()).fold(vec![0; ratings.len()], |mut arr, i| {if i > 0 && ratings[i] > ratings[i - 1] {arr[i] = arr[i - 1] + 1} arr}).iter().enumerate().rev().fold((ratings.len() as i32, 0), |(ans, pre), (i, &v)| {if i < ratings.len() - 1 && ratings[i] > ratings[i + 1] {(ans + max(v, pre + 1), pre + 1)} else {(ans + v, 0)}}).0}
use std::cmp::max;

impl Solution {
    pub fn candy(ratings: Vec<i32>) -> i32 {
        (0..ratings.len())
            .fold(vec![0; ratings.len()], |mut arr, i| {
                if i > 0 && ratings[i] > ratings[i - 1] {
                    arr[i] = arr[i - 1] + 1
                }
                arr
            })
            .iter()
            .enumerate()
            .rev()
            .fold((ratings.len() as i32, 0), |(ans, pre), (i, &v)| {
                if i < ratings.len() - 1 && ratings[i] > ratings[i + 1] {
                    (ans + max(v, pre + 1), pre + 1)
                } else {
                    (ans + v, 0)
                }
            })
            .0
    }
}