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?
Solution 1:
两头扫瞄,第一次从左到右,第二次从右到左,这样递增和递减的关系就刚好相反。每一个递增都加一,递减就不变。
public int candy(int[] ratings) { int n = ratings.length; int[] candy = new int[n]; Arrays.fill(candy, 1); for(int i=1, k=1; i<n; i++) { if(ratings[i] > ratings[i-1]) { candy[i] = ++k; } else { k = 1; } } for(int i=n-2, k=1; i>=0; i--) { if(ratings[i] > ratings[i+1]) { // i.e. [4,2,3,4,1], the previous candy count may be larger than ++k, we should get the larger one. candy[i] = Math.max(candy[i], ++k); } else { k = 1; } } int sum = 0; for(int num : candy) { sum += num; } return sum; }
上面代码中的K其实可以用candy数组中的上个元素的值替代,因为只要比上一个元素值大1就行。
所以代码可以改成这个样子:
public int candy(int[] ratings) { int n = ratings.length; int[] candy = new int[n]; Arrays.fill(candy, 1); for(int i=1; i<n; i++) { if(ratings[i] > ratings[i-1]) { candy[i] = candy[i-1] + 1; } } for(int i=n-2; i>=0; i--) { if(ratings[i] > ratings[i+1]) { // i.e. [4,2,3,4,1], the previous candy count may be larger than candy[i+1]+1, we should get the larger one. candy[i] = Math.max(candy[i], candy[i+1] + 1); } } int sum = 0; for(int num : candy) { sum += num; } return sum; }
Solution 2:
网上很多文章给出了这种解法。我在leetcode上测试了下,会曝出运行超时。
public int candy(int[] ratings) { if (ratings.length == 0) return 0; int[] d = new int[ratings.length]; d[0] = 1; for (int i = 1; i < ratings.length; i++) { if (ratings[i] == ratings[i - 1]) d[i] = 1; else if (ratings[i] > ratings[i - 1]) d[i] = d[i - 1] + 1; else {// should give less candy than prev child d[i] = 1; if (d[i - 1] == 1) { int j = i; while (j > 0 && ratings[j - 1] > ratings[j] && d[j - 1] == d[j]) { //only push backwards when ratings diff but candy are same d[j - 1]++; j--; } } } } int sum = 0; for (int i = 0; i < ratings.length; i++) { sum += d[i]; } return sum; }
相关推荐
"135", // id "titleSlug": "candy", // 题目url "title": "Candy", // 题目标题 "content": "<p>There are <em>N ...", // 题目描述 "difficulty": "Hard", // 难度 ...
candy 贪心 gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca
LeetCode.135分发糖果135. 分发糖果解题思路:3、最后发放糖果时必须同时满足左规则、右规则,所以取左右规则最大值public int candy
leetcode 和 oj #问题列表(按标题升序) 标题|添加日期|AC 利率 ---|---|---|--- 3Sum |2012-01-17|16.4% 3Sum Closest|2012-01-18|26.8% 4Sum|2012-01-26 |21.3% 二进制加法|2012-04-02|25.6% 两个数相加|2011-11-...
candy: minimum_depth_binary_tree: twoSum: regularExpressionMathcing: sameTree: find_content_children: LeetCode 算法题 时间复杂度和空间复杂度权衡,时间复杂度的提升是以空间复杂度为代价的 仔细观察,...
我自己在新学erlang,在LeetCode OJ上找了题目练习,题目很适合新手熟悉语言,但是LeetCode OJ里面只有几门主流语言的答案,下面是已完成的erlang源代码,后续有空再做其他问题续传,题目包含:(源码开头都有题目...
leetcode最大蓄水量 记录数据结构和leetCode算法题 数据结构 sparsearray 稀疏数组 singlequeue 数组模拟队列 circelqueue 数组模拟环形队列 singlequeue 单向链表 doublelink 双向链表 circlesinglelink 环形链表 ...
谷歌师兄的leetcode刷题笔记 甜蜜银行 :candy: Sweet Bank 是Kotlin中的一个 Android 应用程序,我开发它是为了尝试移动开发的一些原则,例如范围分离、设计模式(例如 MVVM)等等。 我使用 Starling Bank API 来...
Candy_leetcode:刷leetcode的代码汇总
leetcode打不开玩具问题 编码练习 string input; while (getline(cin, input)) { cout << "input: " << input << endl; string output = candyCrush(input); cout << "output: " << ...
应用程序三重子序列128最长连续序列164最大间隔桶287查找重复数135Candy很少考330Patching Array很少考 LifeInterval57Insert Interval56Merge Intervals252Meeting Rooms253Meeting客房II352Data流从数据Stream53...
网上搜集的华为面试机试题库#字符串分割#服务器广播# #分子弹,这个题想了好久,最后发现是leetcode上面的candy那一类的#无重复字符的最长字串
leetcode_easy_python 挑战1431:给定数组糖果和整数extraCandies,其中candy [i]代表第i个孩子拥有的糖果数量。 对于每个孩子,检查是否有一种方法可以在孩子之间分配额外的糖果,以便他或她可以在其中拥有最多的...
AlgoHub囊括了 POJ, ZOJ, leetcode, HackerRank 等网站的经典题目(一些质量不高的题目则忽略),且 AlgoHub有非常简单的加题系统,用户不需要写一行代码即可自己添加题目,所以AlgoHub的题库还在飞速增长中。...
3. iterate two candy arrays, sum the max element from two arrays. 4. 只关注增量,如果rat
(C语言是我初恋:candy: ) 目前有的: Java 操作系统 计网 JVM leetcode 计算机基础 Android 设计模式 数据库 工具 面筋 :laptop: :cloud: :ogre: :lion: :globe_showing_Asia-Australia: :robot: :deer: :memo:...