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…
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(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…
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…
历届试题 分糖果 时间限制:1.0s 内存限制:256.0MB 问题描述 有n个小朋友围坐成一圈.老师给每个小朋友随机发偶数个糖果,然后进行下面的游戏: 每个小朋友都把自己的糖果分一半给左手边的孩子. 一轮分糖后,拥有奇数颗糖的孩子由老师补给1个糖果,从而变成偶数. 反复进行这个游戏,直到所有小朋友的糖果数都相同为止. 你的任务是预测在已知的初始糖果情形下,老师一共需要补发多少个糖果. 输入格式 程序首先读入一个整数N(2
575. 分糖果 给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果.你需要把这些糖果平均分给一个弟弟和一个妹妹.返回妹妹可以获得的最大糖果的种类数. 示例 1: 输入: candies = [1,1,2,2,3,3] 输出: 3 解析: 一共有三种种类的糖果,每一种都有两个. 最优分配方案:妹妹获得[1,2,3],弟弟也获得[1,2,3].这样使妹妹获得糖果的种类数最多. 示例 2 : 输入: candies = [1,1,2,3] 输出: 2 解析: 妹妹获得…
昨天晚上 在csdn上做了一道分糖果的题目,我自个测的是没有问题,但是提交答案后,老失败,提示 你的程序正常运行并输出了结果,但是答案错误你的程序输出结果与测试数据中的输出结果不符 我先把自个思路说一下,再把我的代码贴出来,你们帮忙看看,哪里出了问题,或者有更好的思路分享下. 题目是这样的:地址:http://student.csdn.net/mcs/programming_challenges 第二道题目 有n个小朋友站成一排(编号从0到n-1),每个小朋友有一个rating值,存放在rati…