LeetCode:Candy
来源:互联网 发布:js金沙国际 编辑:程序博客网 时间:2024/05/20 16:40
Candy
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?
<span style="font-size:18px;"> int candy(vector<int> &ratings) { vector<int> result(ratings.size(),1); for(int i = 1; i < ratings.size();i++) { if(ratings[i] > ratings[i-1]) result[i] = result[i-1] + 1; } for(int i = ratings.size()-2;i>=0;i--) { if(ratings[i]>ratings[i+1]) { result[i] = result[i] >= result[i+1] + 1 ? result[i] : result[i+1] + 1; } } int num = 0; for(int i = 0; i < ratings.size();i++) { num+=result[i]; } return num; }</span>分别从前向后、从后向前修正结果
已AC
0 0
- Leetcode: Candy
- LeetCode:Candy
- [leetcode]Candy
- Leetcode: Candy
- 【leetcode】Candy
- LeetCode:Candy
- [LeetCode] Candy
- [LeetCode]Candy
- [LeetCode] Candy
- 【LeetCode】Candy
- LeetCode | Candy
- Leetcode: Candy
- Candy - LeetCode
- leetcode Candy
- LeetCode Candy
- candy leetcode
- Candy -- LeetCode
- [LeetCode] Candy
- android:configChanges="orientation"使用中应该注意的问题
- android share
- [ExtJS5学习笔记]第二十节 Extjs5配合数组的push方法,动态创建并加载组件
- 自己收集些有用的东西
- 多线程下载文件
- LeetCode:Candy
- Canvas数据转图片
- 线程生命周期
- 问题5:RAC + ASM环境下,多路复用控制文件
- 服务器Button执行js方法
- 谷歌开源项目Chromium的源码获取与项目构建(Win7+vs10/vs13)
- 二叉排序树的创建,删除结点;树的前序,中序,后序非递归遍;二叉树的线索化
- C++ - 继承虚函数调用 代码
- 寻找二叉树两个结点的最低共同父节点