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 ...
随机推荐
- 2018年全国多校算法寒假训练营练习比赛(第二场)F - 德玛西亚万岁
链接:https://www.nowcoder.com/acm/contest/74/F来源:牛客网 题目描述 德玛西亚是一个实力雄厚.奉公守法的国家,有着功勋卓著的光荣军史. 这里非常重视正义.荣耀 ...
- Spring @RequestMapping 注解使用技巧
@RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...
- [ 转载 ] Java开发中的23种设计模式详解(转)
Java开发中的23种设计模式详解(转) 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类 ...
- django使用admin
1.在应用下的admin.py添加 #!/usr/bin/python # coding:utf-8 from django.contrib import admin from .models imp ...
- python orm字段解析
null # 是否可以为空 default # 默认值 primary_key # 主键 db_column # 列名 db_index # 索引(db_index=True) unique # 唯一 ...
- 腾讯云服务器无法ssh登陆问题
SSH 登录时出现如下错误:Permission denied, please try again 解决:腾讯云主机控制台登录,先要设置root密码 修改 /etc/ssh/sshd_config 中 ...
- ARC 101 D - Median of Medians
题面在这里! 这种题只能二分答案把qwwq,直接做根本做不了啊... 首先你需要知道如何通过 一个区间<=x的数有多少个 来判断x和这个区间中位数的关系. 很显然当数有至少 [L/2]+1 个( ...
- PHP--SPL扩展学习笔记
一. SPL是干嘛的 SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 数据结构: .实现双向列表 SplDoublyLinkedList implements I ...
- Codeforces Round #279 (Div. 2) B - Queue 水题
#include<iostream> #include<mem.h> using namespace std; ],q[]; int main() { int n,x,y; m ...
- 配置主从Mysql
怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3.修改root的密码 2.修 ...