问题描述:
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?
原问题链接:https://leetcode.com/problems/candy/
问题分析
这个问题比较困难,难在怎么找到满足最小值的条件。给定一个数组来说,它里面的元素值的分布不一定是规则的,所以可能如下面的图形所示:
而这个时候,如果我们需要去求它的最小分布值,可以这么来考虑。对于所有的元素来说,它对应的初始值都是1。对于每个元素来说,我们需要去比较它和它周边的值,如果它周边的值比它大。那么那个比它大的值就在它对应值的基础上加一。对于我们循环遍历来说,一般是从一头到另一头,那么这个时候比较好判断比如说从左到右递增的序列,对于碰到的后面比前面大的元素,都在前面的基础上加一。可是对于从前往后来说递减的元素,这里就不能处理了。
这时候,我们从前往后的过程正好实现了对递增的序列累加。而对于递减的序列来说,从相反的方向来看,它其实也可以说是递增的。那么只要有元素值的增减,它们也应该满足前面的需求。我们也可以从后往前,针对这种情况进行调整。只不过这个时候,我们要选择的值是当前值candies[i] 和candies[i + 1] + 1中最大的那个。
这样,我们在最后再把整个数组里元素相加,就得到最后的结果了。详细的代码实现如下:
public class Solution { public int candy(int[] ratings) { int n = ratings.length; int[] candies = new int[n]; for(int i = 0; i < n; i++) candies[i] = 1; for(int i = 1; i < n; i++) { if(ratings[i] > ratings[i - 1]) { candies[i] = candies[i - 1] + 1; } } for(int i = n - 2; i >= 0; i--) { if(ratings[i] > ratings[i + 1]) { candies[i] = Math.max(candies[i], candies[i + 1] + 1); } } int sum = 0; for(int i = 0; i < n; i++) sum += candies[i]; return sum; } }
相关推荐
Candy_leetcode:刷leetcode的代码汇总
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-...
leetcode 力码 【吾生也有涯,而知也无涯。】 刷题解题记录: 大批: 220 包含重复 III 考 55 跳跃游戏45 跳跃游戏 II121 买卖股票的最佳时机 122 买卖股票的最佳时机 II123 买卖股票的最佳时机 III188 买卖股票的...
我自己在新学erlang,在LeetCode OJ上找了题目练习,题目很适合新手熟悉语言,但是LeetCode OJ里面只有几门主流语言的答案,下面是已完成的erlang源代码,后续有空再做其他问题续传,题目包含:(源码开头都有题目...
leetcode中国 leetcode-chinese-crawler 此代码用于USTC 2019秋季学期web课程开放实验 概述 爬取 LeetCode中文试题。 运行环境 Windows 10 python 3; 库: requests requests_toolbelt codecs json time random json...
candy: minimum_depth_binary_tree: twoSum: regularExpressionMathcing: sameTree: find_content_children: LeetCode 算法题 时间复杂度和空间复杂度权衡,时间复杂度的提升是以空间复杂度为代价的 仔细观察,...
LeetCode.135分发糖果135. 分发糖果解题思路:3、最后发放糖果时必须同时满足左规则、右规则,所以取左右规则最大值public int candy
:candy: Sweet Bank 是Kotlin中的一个 Android 应用程序,我开发它是为了尝试移动开发的一些原则,例如范围分离、设计模式(例如 MVVM)等等。 我使用 Starling Bank API 来获取数据并将其发送到沙箱:这确实帮助我...
candy 贪心 gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca
leetcode最大蓄水量 记录数据结构和leetCode算法题 数据结构 sparsearray 稀疏数组 singlequeue 数组模拟队列 circelqueue 数组模拟环形队列 singlequeue 单向链表 doublelink 双向链表 circlesinglelink 环形链表 ...
leetcode_easy_python 挑战1431:给定数组糖果和整数extraCandies,其中candy [i]代表第i个孩子拥有的糖果数量。 对于每个孩子,检查是否有一种方法可以在孩子之间分配额外的糖果,以便他或她可以在其中拥有最多的...
(C语言是我初恋:candy: ) 目前有的: Java 操作系统 计网 JVM leetcode 计算机基础 Android 设计模式 数据库 工具 面筋 :laptop: :cloud: :ogre: :lion: :globe_showing_Asia-Australia: :robot: :deer: :memo:...
leetcode打不开玩具问题 编码练习 string input; while (getline(cin, input)) { cout << "input: " << input << endl; string output = candyCrush(input); cout << "output: " << ...
网上搜集的华为面试机试题库#字符串分割#服务器广播# #分子弹,这个题想了好久,最后发现是leetcode上面的candy那一类的#无重复字符的最长字串
3. iterate two candy arrays, sum the max element from two arrays. 4. 只关注增量,如果rat
AlgoHub囊括了 POJ, ZOJ, leetcode, HackerRank 等网站的经典题目(一些质量不高的题目则忽略),且 AlgoHub有非常简单的加题系统,用户不需要写一行代码即可自己添加题目,所以AlgoHub的题库还在飞速增长中。...