1 题目

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?

2 思路

按照网上的思路,每个孩子至少有一个糖果,先从左到右遍历一遍,写出递增的糖果数,再从右到左遍历一遍完成递减的糖果数。这种大小与左右两边数据相关的问题,均可以采用这个思路。另外,有另一种空间复杂度O(1),时间复杂度O(n)的思路,可以参考http://www.cnblogs.com/felixfang/p/3620086.html

3 代码

 public int candy(int[] ratings) {
if(ratings == null || ratings.length == 0)
{
return 0;
}
int[] candyNums = new int[ratings.length];
candyNums[0] = 1; for(int i = 1; i < ratings.length; i++)
{
if(ratings[i] > ratings[i-1]) //如果第i个孩子比第i - 1孩子等级高,
{
candyNums[i] = candyNums[i-1]+1;
}
else //每人至少有一个糖果
{
candyNums[i] = 1;
}
} for(int i = ratings.length-2; i >= 0; i--)
{ if(ratings[i] > ratings[i + 1] && candyNums[i] <= candyNums[ i + 1]) //如果第i个孩子比第i + 1孩子等级高并且糖果比i+1糖果少
{
candyNums[i] = candyNums[i + 1] + 1;
}
} int total = 0;
for (int i = 0; i < candyNums.length; i++) {
total += candyNums[i];
} return total;
}

[leet code 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. 【Leet Code】Palindrome Number

    Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...

  3. Leet Code 771.宝石与石头

    Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S  ...

  4. leetcode 135. Candy ----- java

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

  5. Leetcode#135 Candy

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

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

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

  7. 135. Candy

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  8. 【LeetCode】135. Candy

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

  9. 135. Candy(Array; Greedy)

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

随机推荐

  1. [Robot Framework] Robot Framework用Execute Javascript对XPath表示的元素执行Click操作

    Execute Javascript document.evaluate("//a[contains(@href,'createBook')]", document, null, ...

  2. Spark学习笔记-GraphX-1

    Spark学习笔记-GraphX-1 标签: SparkGraphGraphX图计算 2014-09-29 13:04 2339人阅读 评论(0) 收藏 举报  分类: Spark(8)  版权声明: ...

  3. Windows 8.1 app 界面设计

    大纲: Windows 应用商店应用 UI 详细信息 http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/dn263191.aspx 快 ...

  4. [ASP.NET]使用Oracle.ManagedDataAccess的OracleParameter参数化和OracleDataAdapter模糊查询

    今天写个查询员工的信息的demo遇到了2个问题 问题1.使用Oracle.ManagedDataAccess的OracleParameter参数化 OracleParameter 的使用(参数名要以: ...

  5. spring学习 十五 spring的自动注入

    一  :在 Spring 配置文件中对象名和 ref=”id” ,id 名相同使用自动注入,可以不配置<property/>,对应的注解@Autowired的作用 二: 两种配置办法 (1 ...

  6. Apache Prefork、Worker和Event三种MPM分析

    三种MPM介绍 Apache 2.X  支持插入式并行处理模块,称为多路处理模块(MPM).在编译apache时必须选择也只能选择一个MPM,对类UNIX系统,有几个不同的MPM可供选择,它们会影响到 ...

  7. codeforces C. Functions again

    题意:给定了一个公式,让你找到一对(l,r),求解出公式给定的F值. 当时没有想到,我把(-1)^(i-l)看成(-1)^i,然后思路就完全错了.其实这道题是个简单的dp+最长连续子序列. O(n)求 ...

  8. android开发笔记(1)

    最近老师要求我们使用android开发一些东西.但是对我们而言,android是一个未知的方面.先说说我对于android的软件的基本认识,首先他很难,因为他是一个未知的领域:其次,我们只是掌握了一些 ...

  9. Java基础语法学习知识

    基础概念 标识符1.由字母,数字,下划线,美元符组成2.首字母不能是数字3.不能是关键字和保留字4.能反映其作用 关键字1.有特定含义2.用于特定地方3.用来命名标识符 常量1.程序执行时值不变的量2 ...

  10. springboot 容器启动事件

    在springboot 容器启动时,我们需要在启动过程中做一些操作,比如启动容器后,执行某些代码. spring 提供了监听器,我们可以方便的实现这些操作. 在容器启动开始时: package com ...