candy

[LeetCode] Candy, Solution

狂风中的少年 提交于 2020-04-07 05:43:06
There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies than their neighbors. What is the minimum candies you must give? [Thoughts] 蛮好玩的题。感觉用dp简单点。定义Candy[i]为第i个孩子需要给的最少的糖数, 那么 Candy[i] = Candy[i-1]+1 if ratings[i] > ratings[i-1] 递增序列,后面小孩需要的糖果是前一个小孩的糖果数+1 1 if ratings[i] == ratings[i-1] 直线,按照题意,如果两个小孩rating一样多,后面的小孩可以只拿一个糖 Candy[i-1] –1 if ratings[i] < ratings[i-1] 递减序列。这个递推式显然是有缺陷,因为如果递减序列比较长的话,Candy

lc面试准备:Candy

徘徊边缘 提交于 2020-04-05 22:21:07
1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies than their neighbors. What is the minimum candies you must give? 接口 int candy(int[] ratings) 几个小孩站一排,每个小孩有个等级值,现在给小孩发糖,发的时候要遵守2个规则:(1)每个小孩至少一颗糖(2)两个相邻的小孩中,等级大的小孩一定比等级小的小孩糖多,求发糖的数目的最小值。 2 思路 基本思路:先进行2次扫描,一次从左往右,一次从右往左。最后一次扫描累加出结果。 第一次扫描:维护对于每一个小孩左边所需要最少的糖果数量,存入数组对应元素中。 第二次扫描:维护右边所需的最少糖果数。 第三次扫描:将左边和右边大的糖果数量累加到result,从而累加得出结果。 example: ratings =

Codeforce Round #574(Div.2)

半世苍凉 提交于 2019-11-27 04:52:21
A. Drinks Choosing time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated? There are n n students living in a building, and for each of them the favorite drink a i ai is known. So you know n n integers a 1 , a 2 , … , a n a1,a2,…,an, where a i ai ( 1 ≤ a i ≤ k 1≤ai≤k) is the type of the favorite drink of the i i-th student. The drink types are