Leetcode solution 135: Candy Blogger:https://blog.baozitraining.org/2019/11/leetcode-solution-135-candy.html 博客园: https://www.cnblogs.com/baozitraining/p/11747009.html B站: https://www.bilibili.com/video/av73575024/ Problem Statement There are N children standing in a line. Each child...
Solution: This problem seemed vague to me at first, as the key sentence in the problem:Children with a higher rating get more candies than their neighbors.It didn't specify the case for neighbors with equal rates. For the sample case {7, 8, 4}, the result will be {1, 2, 1}. Whi...
candies than their neighors, but if two child has the same rating, we do not need to give more candies the time complexity is O(n), the space complexity is also O(n) 1publicclassSolution {2publicintcandy(int[] ratings) {3intN =ratings.length;4if(N == 0){5returnN;6}7int[] c...
题目链接:https://leetcode.com/problems/candy/ 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果。 相邻的孩子中,评分高的孩子必须获得更多的糖果。 那么这样下来,老师至少需要准备多...
Candy -- LeetCode 原题链接: http://oj.leetcode.com/problems/candy/ 这道题用到的思路和Trapping Rain Water是一样的,用动态规划。基本思路就是进行两... Leetcode - Candy My code: reference: https://discuss.leetcode.com/topic/5243/a-simple-solution/3 Greedy. 直接看的答案。到现在也还不...
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 Key to the problem is to consider the whole array as combination of many ascending sub-arrays and descending sub-arrays. So when...