candy Java code
来源:互联网 发布:excel怎么更新数据 编辑:程序博客网 时间:2024/05/17 07:51
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?
import java.util.*;public class Solution { public int candy(int[] ratings) { int n = ratings.length; if(n==0 || ratings == null) return 0; int[] count = new int[n]; int sum=0; //初始,每个孩子至少有一颗糖 Arrays.fill(count, 1); for(int i=1; i<n; i++){ if(ratings[i] > ratings[i-1]){ count[i] = count[i-1]+1; } } for(int i=n-1; i>0; i--){ if(ratings[i]<ratings[i-1] && count[i]>=count[i-1]){ count[i-1] = count[i]+1; } sum += count[i]; } sum += count[0]; return sum; }}
阅读全文
0 0
- candy Java code
- CODE 132: Candy
- Candy Java
- Candy (Java)
- Candy Java
- [Leetcode] Candy (Java)
- [leetcode] Candy Java
- [LeetCode][Java] Candy
- [leetcode-135]Candy(java)
- leetcode:Candy 【Java】
- 【leetcode】135. Candy【java】
- [LeetCode] 135. Candy java
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Git常用命令行(一)
- JDBC
- iOS
- JavaWeb起步
- WPF程序启动后资源管理器应用程序无法显示
- candy Java code
- 实验报告五:用callback增强链表模块来实现命令行菜单小程序V2.8
- 第1108期AI100_机器学习日报(2017-09-30)
- 阿里面试回来,想和Java程序员谈一谈
- mongoose入门
- 基于 canvas 实现的一个截图小 demo
- Innodb存储引擎的特性
- 基于react的影院购票应用
- Javascript之bind