[leet code 135]candy
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的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- 135. Candy
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 135. Candy(Array; Greedy)
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- MCS-51与8086指令系统比较
- Autofs
1. Introduction autofs is a program for automatically mounting directories on an as-needed basis. Au ...
- [Chrome Headless + Python] 截长图 (Take Full-page Screenshot)
# -*- coding: utf-8 -*- import time import os from selenium import webdriver from selenium.webdriver ...
- JDK 规范目录
JDK 规范目录 1.1 Java 异常处理 2.1 JDK 之 NIO 2 WatchService.WatchKey(监控文件变化) https://mp.weixin.qq.com/s/NIn2 ...
- robotframework 运行集合
Robot Framework 运行测试通过 pybot 命令,检查 _C:\Python36\Scripts_ 目录下是否有 pybot.bat 文件,正确安装 Robot Framework 一定 ...
- 动态代理jdk和cglib的区别
学习来源贴:http://www.cnblogs.com/jqyp/archive/2010/08/20/1805041.html JDK实现动态代理需要实现类通过接口定义业务方法,对于没有接口的类, ...
- 谷歌开源OCR,tesseract-ocr使用笔记
官方教程地址:https://github.com/tesseract-ocr/tesseract/wiki/Compiling 测试版本为 root@9a2a063f9534:/tesseract/ ...
- 720. Longest Word in Dictionary
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- PHP中require(),include(),require_once()和include_once()有什么区别
引用文件的方法有两种:require 及 include.两种方式提供不同的使用弹性. require 的使用方法如 require("MyRequireFile.php"); . ...
- 简单实现java线程池
使用多线程以及线程池的意义无需多说,要想掌握线程池,最好的方法还是自己手动去实现. 一.实现思路 (网络盗图) 二.实现代码 1.线程池类 package com.ty.thread; im ...