Java for LeetCode 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?
解题思路:
双向爬坡问题,从前往后、从后往前各扫一遍,JAVA实现如下:
public int candy(int[] ratings) {
int sum = 0;
int[] candies = new int[ratings.length];
for (int i = 0; i < ratings.length - 1; i++)
if (ratings[i] < ratings[i + 1])
candies[i + 1] = candies[i] + 1;
for (int i = ratings.length - 2; i >= 0; i--)
if (ratings[i + 1] < ratings[i])
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
for (int i = 0; i < candies.length; i++)
sum += 1 + candies[i];
return sum;
}
Java for LeetCode 135 Candy的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- Java实现 LeetCode 135 分发糖果
135. 分发糖果 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果. ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- [leetcode] 135. Candy (hard)
原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
随机推荐
- mysqldump实践
mysqldump mysqldump---逻辑备份,热备 单线程,适合数据量小的库 mysql官方自带的命令行工具 #全库 )mysqldump -uroot -p123456 --sock ...
- 深入浅出 Cocoa 之 Core Data(2)- 手动编写代码
深入浅出 Cocoa 之 Core Data(2)- 代码示例 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 前面详细讲解了 Core Data 的框 ...
- VS2010 MFC中 Date Time Picker控件的使用
1. 在工具箱中找到Date Time Picker控件,然后拖放到对话框上. 2. 在其属性中按自己的需求做一些设置. Format 属性:Long Date (长日期):****年**月**日 S ...
- nginx phase handler的原理和选择
nginx phase handler的原理和选择 PHASE HANDLER的种类 nginx在接收并解析完请求行.请求头之后.就会依次调用各个phase handler. phase handle ...
- 压缩软件Snappy的安装
1.下载源码,通过编译源码安装 tar -zxvf /home/zfll/soft/snappy-1.1.2.tar.gz cd snappy-1.1.2 ./configure make sud ...
- 【重点突破】——使用Canvas进行绘图图像
一.引言 本文主要是canvas绘图中绘制图像的部分,做了几个练习,综合起来,复习canvas绘图以及定时器的使用. 二.canvas绘制小飞机在指定位置 <!DOCTYPE html> ...
- UTF-8 可变编码格式
转自:http://blog.csdn.net/swedenfeng/article/details/53467720 UTF-8 是一种可变编码格式,长度从一个字节到四个字节,可根据UTF-8字 ...
- sublime添加sass编译
首先安装Ruby环境sass是基于ruby的产物,因此在安装sass前需要先安装ruby,如果用命令方式编译Sass也是必须安装ruby的.命令行编译sass见!下载Ruby windows 安装包: ...
- CSS3 background属性
background: #00FF00 url(bgimage.gif) no-repeat fixed top; background 简写属性在一个声明中设置所有的背景属性. 可以设置如下属性: ...
- 转Java 开发环境配置
window系统安装java 下载JDK 首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloa ...