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的更多相关文章

  1. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  2. leetcode 135. Candy ----- java

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  4. Java实现 LeetCode 135 分发糖果

    135. 分发糖果 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果. ...

  5. Leetcode#135 Candy

    原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...

  6. [leetcode] 135. Candy (hard)

    原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...

  7. 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 ...

  8. 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. ...

  9. 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 ...

随机推荐

  1. AIX 删除指定目录、指定后缀、指定天数以前的历史文件

    find /home/oracle/admin/zhjport/udump/ -name "*.trc" -mtime +30 -exec rm {} \; 如果要自动执行可以加入 ...

  2. golang--- Redis 操作

    1. Redis简介 Redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value数据库.   1.1 特点 支持更多数据类型 和Memcached类似,它支持存 ...

  3. 队列容易queue

    /*先进先出*/#include<iostream>#include<queue> //使用队列必须包含头文件using namespace std;int main(){ q ...

  4. PCA原理

    http://blog.csdn.net/shizhixin/article/details/51181379

  5. Web支持可暂停的超大文件上传

    代码镇顶:https://github.com/dna2github/petalJS/blob/master/upload 前些天遇到用户须要上传10GB大小以上的文件的需求,查查网上的库.都不好用. ...

  6. git 指令如何撤销一次merge

    在使用git指令时难免会发生错误的merge的情况,那么如何在这种情况下回退到错误发生之前的情况? git reflog 指令显示历史的操作 4457e43 HEAD@{0}: reset: movi ...

  7. html小知识,怎么实现一个td占据2行

    <table border="1" width="100%"> <tr> <td rowspan="2"> ...

  8. linux驱动开发重点关注内容--摘自《嵌入式Linux驱动模板精讲与项目实践》

    本文摘自本人拙著 <嵌入式Linux驱动模板精讲与项目实践> 初步看起来Linux设备驱动开发涉及内容非常多,而须要实现驱动的设备千差万别.事实上做一段时间驱动之后回首看来主要就是下面几点 ...

  9. scramble-string——两个字符串经过树化并旋转后是否一致、递归、动态规划

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  10. TCP/IP协议(数据封装与拆装过程)

      IP地址 = 网络地址+主机地址  = 网络地址 + 子网地址 + 主机地址 应用进程之间的通信被称之为端到端的通信. 传输层与网络层之间的区别:传输层为应用进程间提供了端到端的逻辑通信:网络层提 ...