博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Candy
阅读量:4075 次
发布时间:2019-05-25

本文共 1055 字,大约阅读时间需要 3 分钟。

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 class Solution {    public int candy(int[] ratings) {        int len = ratings.length;		if (0 == len)			return 0;		if (1 == len)			return 1;		int num = 0;		int[] candys = new int[len];		for (int i = 0; i < len; i++)			candys[i] = 1;		for (int i = 0; i < len - 1; i++) {			if (ratings[i] < ratings[i + 1])				candys[i + 1] = candys[i] + 1;		}		for (int i = len - 1; i > 0; i--) {			if (ratings[i] < ratings[i - 1])				if (i != 1){					if (ratings[i - 2] >= ratings[i - 1])						candys[i - 1] = candys[i] + 1;					else						candys[i - 1] = Math.max(candys[i - 2], candys[i]) + 1;				}else{					candys[i - 1] = candys[i] + 1;				}		}		int sum = 0;		for (int i = 0; i < len; i++) {			sum += candys[i];		}		return sum;    }}
 

转载地址:http://hsuni.baihongyu.com/

你可能感兴趣的文章
IIS7.5中神秘的ApplicationPoolIdentity
查看>>
HOLOLENS的DEVICE POTAL连接和安装
查看>>
HOLOLENS如何调节屏幕亮度和音量?
查看>>
HOLOTOOLKIT的使用
查看>>
Unity Camera的两种模式
查看>>
Collider Collision 区别
查看>>
博客推荐
查看>>
UNITY3D中涉及的一些数学知识
查看>>
Unity3D粒子系统 合集
查看>>
UNITY在VS中调试
查看>>
Unity StartCoroutine 和 yield return 深入研究
查看>>
Unity-Animator深入系列
查看>>
UNITY3D的变量初始化问题
查看>>
UNITY3D单词学习 speed和velocity的区别
查看>>
Unity3D中的线性插值Lerp()函数解析
查看>>
pitch yaw roll 的区别
查看>>
什么是UV?
查看>>
手机开启HDR后拍照有什么不同?
查看>>
图文详解Unity3D中Material的Tiling和Offset是怎么回事
查看>>
voxel 与 pixel
查看>>