leetcode 135. Candy
来源:互联网 发布:qq密码查询数据库 编辑:程序博客网 时间:2024/06/05 19:31
135. 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?
本题的关键在于如果ratings小,那么拿到的candy就比两边少,所以应该想到左右两个方向各来一次遍历。
class Solution {public: int candy(vector<int>& ratings) { int n = ratings.size(); vector<int> A(n, 1); for (int i = 1; i < n; i++) { if (ratings[i] > ratings[i-1]) A[i] = A[i-1] + 1; } for (int i = n - 2; i >= 0; i--) { if (ratings[i] > ratings[i+1] && A[i] < A[i+1] + 1) A[i] = A[i+1] + 1; } return accumulate(A.begin(), A.end(), 0); }};
阅读全文
0 0
- [LeetCode]135.Candy
- 135. Candy Leetcode Python
- [Leetcode] 135. Candy
- [leetcode] 135.Candy
- [leetcode] 135. Candy
- leetcode 135. Candy
- leetcode 135. Candy
- [LeetCode]135. Candy
- LeetCode 135. Candy
- Leetcode 135. Candy
- leetcode.135. Candy
- LeetCode 135. Candy
- [leetcode] 【数组】 135. Candy
- LeetCode-135.Candy
- LeetCode 135. Candy
- Leetcode 135. Candy
- leetcode-135. Candy
- [LeetCode] 135. Candy
- Syncro.SVN.Client.v5.1.
- 云星数据---Scala实战系列(精品版)】:Scala入门教程024-Scala实战源码-Scala abstract 抽象类01
- eclipse中使用lombok,优化java代码
- HDFS运行原理
- Cal:一个封装类,利用方法对数组进行各种操作
- leetcode 135. Candy
- 用 Python 实现一个大数据搜索引擎
- ios基础:iOS改变状态栏字体颜色
- HTML中 &emsp等空格的区别
- Android性能优化优秀博客集锦
- Ubuntu系统如何安装软件
- Mat类操作
- 云坤“互联网+企业公共服务平台”解决方案
- 很不错的外贸站测速工具