[Leetcode] Candy (Java)
来源:互联网 发布:淘宝直通车 转化率多少 编辑:程序博客网 时间:2024/05/16 04:37
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?
发糖果
从两边各遍历一遍
public class Solution { public int candy(int[] ratings) { if(ratings.length==0)return 0;int ret = ratings.length;int[] candy = new int[ratings.length];int k=1;for(int i=1;i<candy.length;i++){if(ratings[i]>ratings[i-1])candy[i]=k++;else k=1;}k=1;for(int i=candy.length-2;i>=0;i--){if(ratings[i]>ratings[i+1])candy[i]=Math.max(k++,candy[i]);else k=1;}for(int i=0;i<candy.length;i++){ret+=candy[i];}return ret; }}
0 0
- [Leetcode] Candy (Java)
- [leetcode] Candy Java
- [LeetCode][Java] Candy
- [leetcode-135]Candy(java)
- leetcode:Candy 【Java】
- 【leetcode】135. Candy【java】
- [LeetCode] 135. Candy java
- Leetcode: Candy
- LeetCode:Candy
- [leetcode]Candy
- Leetcode: Candy
- 【leetcode】Candy
- LeetCode:Candy
- [LeetCode] Candy
- [LeetCode]Candy
- [LeetCode] Candy
- 【LeetCode】Candy
- LeetCode | Candy
- distmsh网格划分之长方形含边界裂纹
- CentOS6.5系统挂载NTFS分区的移动硬盘
- linux上遇到的小问题
- Cocos2d-x 让精灵图像变灰的方法
- 只读事务的一些概念
- [Leetcode] Candy (Java)
- [JQuery]怎样获取和修改文本框的值
- c++获取系统当前时间
- 几种web报表打印方案的比较
- FW: diff between =null and is NULL in sql
- 使用ajax上传/下载文件方法
- Unity3d策略模式
- ARM软件优化,从1s到10ms
- 露眼看App--怎样做一款旅游类的App ?