Candy leetcode java
题目:
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?
题解:
这道题和Trapping water那个是一样的想法,因为无论是水坑还是得到糖的小朋友,影响因素都不只一边,都是左右两边的最小值/最大值来决定的。
所以这道题跟上一道一样,也是左右两边遍历数组。
leftnums数组存从左边遍历,当前小朋友对比其左边小朋友,他能拿到糖的数量;
rightnums数组存从右边遍历,当前小朋友对比其右边小朋友,他能拿到的糖的数量。
最后针对这两个数组,每个小朋友能拿到的糖的数量就是这两个数最大的那个数,求总加和就好了。
代码如下:
1 public int candy(int[] ratings) {
2 if(ratings==null || ratings.length==0)
3 return 0;
4
5 int[] leftnums = new int[ratings.length];
6 int[] rightnums = new int[ratings.length];
7
8 leftnums[0]=1;
9 for(int i=1;i<ratings.length;i++){
if(ratings[i]>ratings[i-1])
leftnums[i] = leftnums[i-1]+1;
else
leftnums[i] = 1;
}
rightnums[ratings.length-1] = leftnums[ratings.length-1];
for(int i=ratings.length-2;i>=0;i--){
if(ratings[i]>ratings[i+1])
rightnums[i] = rightnums[i+1]+1;
else
rightnums[i] = 1;
}
int res = 0;
for(int i = 0; i<ratings.length; i++)
res += Math.max(leftnums[i],rightnums[i]);
return res;
}
Candy leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- *candy——leetcode
/* */ #include<iostream> #include<vector> //#include<algorithm> #include <windo ...
- 【目录】LeetCode Java实现
这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
随机推荐
- 交换机高级特性MUX VLAN
MUX VLAN 基本概念 lMUX VLAN(Multiplex VLAN)提供了一种通过VLAN进行网络资源控制的机制. 例如,在企业网络中,企业员工和企业客户可以访问企业的服务器. 对于企业来说 ...
- redis 客户端命令
Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接 1 .CLIENT LIST 返回连接到 redis 服务的客户端列表 2 .CLIENT SETNA ...
- BZOJ 2843: 极地旅行社 lct splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...
- BZOJ.5329.[SDOI2018]战略游戏(圆方树 虚树)
题目链接 显然先建圆方树,方点权值为0圆点权值为1,两点间的答案就是路径权值和减去起点终点. 对于询问,显然可以建虚树.但是只需要计算两关键点间路径权值,所以不需要建出虚树.统计DFS序相邻的两关键点 ...
- MySQL主从复制(Replication)(MySQL数据同步)配置
MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器(主服务器master)复制数据到另一个服务器(从服务器slave)的一个进程. 配置主服务器(mast ...
- Linux怎么开启ssh
一.查看ssh开启状态 service ssh status 这是已经开启了的状态 二.如果没有开启 键入以下命令开启 service ssh start 三.开启后如果不能利用xshell远程访问 ...
- HDU 5700 区间交 离线线段树
区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...
- hdu 1199 Color the Ball 离散线段树
C - Color the Ball Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- MySQL数据库基准压力测试工具之MySQLSlap使用实例
一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和 ...
- 重温PHP之快速排序
基本原理:选出当前数组中任一元素(通常为第一个)作为标准,新建两个空数组分别置于当前数组前后,然后遍历当前数组,如果数组中元素值小于等于第一个元素值就放到前边空数组,否则放到后边空数组. //快速排序 ...