[LeetCode] Candy
2015-07-27 00:32
381 查看
Well, you may need to run some examples to have the intuition for the answer since we only require children with higher rating get more candies than their neighbors, not all those with lower ratings.
The following code is taken from this link. It involves two-pass scan to ensure the above condition. You will get it after running some examples, like modifying the code and check the wrong cases :-)
The following code is taken from this link. It involves two-pass scan to ensure the above condition. You will get it after running some examples, like modifying the code and check the wrong cases :-)
class Solution { public: int candy(vector<int>& ratings) { int n = ratings.size(); vector<int> candies(n, 1); for (int i = 1; i < n; i++) if (ratings[i] > ratings[i - 1]) candies[i] = candies[i - 1] + 1; for (int i = n - 1; i > 0; i--) if (ratings[i - 1] > ratings[i]) candies[i - 1] = max(candies[i - 1], candies[i] + 1); int total = 0; for (int i = 0; i < n; i++) total += candies[i]; return total; } };
相关文章推荐
- AsyncHttpClient
- windows安装 MongoDB 3.0.4
- 如何删除xcode项目中不再使用的图片资源
- 我的前端架构之一--页面作用域
- Vim命令合集
- Maven 中央(或国内)仓库地址
- android ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法
- 1.计算机原理实验及实现
- 【jQuery】复选框的批量处理:全选、非全选
- hihocoder 1191 小W与网格
- vim常用命令总结
- POJ 3071 Football 【概率DP】
- Mac OS X开机启动Path had bad permissions错误解决方案
- 数据结构(二):线性表的使用原则以及链表的应用-稀疏矩阵的三元组表示
- sort
- window中的常见方法
- 超过130个你需要了解的vim命令
- 大型网站技术架构
- 插入排序(java)
- 病毒侵袭