/**
* Source : https://oj.leetcode.com/problems/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?
*
*/
public class Candy { /**
* 给站成一排的孩子分糖果,
* 每个孩子至少一个
* 每个孩子的权重不一样,权重高的孩子比相邻的孩子要多
* 求最少需要多少个糖果
*
* 从左到右遍历,寻找递增序列,该位置对应的孩子分的糖果数也递增,每个递增序列从1开始,这样保证了rating高的孩子比左边的孩子多
* 然后从右向左遍历,确保当前位置孩子的糖果数多于右边孩子的
* 复杂度为O(n)
*
* @param ratings
* @return
*/
public int candy (int[] ratings) {
if (ratings.length <= 0) {
return 0;
}
int[] candy = new int[ratings.length];
candy[0] = 1;
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i-1]) {
candy[i] = candy[i-1] + 1;
} else {
candy[i] = 1;
}
}
int sum = candy[ratings.length-1];
for (int i = ratings.length-2; i > -1; i--) {
if (ratings[i] > ratings[i+1] && candy[i] <= candy[i+1]) {
candy[i] = candy[i+1] + 1;
}
sum += candy[i];
}
return sum;
} public static void main(String[] args) {
Candy candy = new Candy();
int[] ratings = new int[]{5, 6, 7, 4, 1, 2, 3, 2, 1, 7};
System.out.println(candy.candy(ratings) + " == 19");
}
}

leetcode — candy的更多相关文章

  1. [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

    [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...

  2. [LeetCode] Candy 分糖果问题

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

  3. [LeetCode] Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  4. [leetcode]Candy @ Python

    原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child ...

  5. Leetcode Candy

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

  6. LeetCode: Candy 解题报告

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

  7. [Leetcode] candy 糖果

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

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

随机推荐

  1. pip install

    pip install <包名> 或 pip install -r requirements.txt 通过使用 == >= <= > < 来指定版本,不写则安装最新 ...

  2. S0.4 二值图与阈值化

    目录 二值图的定义 二值图的应用 阈值化 二值化/阈值化方法 1,无脑简单判断 opencv3函数threshold()实现 2,Otsu算法(大律法或最大类间方差法) OpenCV3 纯代码实现大津 ...

  3. msxfs.dll函数加载代码

    msxfs.dll函数加载代码 #include "stdafx.h" #include "WSXFSLoader.h" NS_AWP_DEVICE_WOSA_ ...

  4. H5测试点总结-UI测试、功能测试、兼容性测试、体验相关(弱网、资源、手机操作等)、安全性测试、性能测试

    一.概述 1.1 什么是H5 H5 即 HTML5,是最新的 Web 端开发语言版本,现如今,大多数手机 APP 页面会用 H5 实现,包括 PC Web 站点也会用它开发实现.所以 Web 的通用测 ...

  5. Web开发者の实用代码账簿

    介里就都是恶魔菌整理的我平时会用的代码啦-现在在这里总结规划一下,希望能对你以及其他阅读这篇文章的小可耐们有帮助喵!欢迎订阅我的博客来get恶魔菌记事簿的新动态鸭! ↓ ↓ ↓ 以下就是内容啦~记得看 ...

  6. python爬虫第六天

        今天继续学习一些实战爬虫   链接爬虫实战       要求:把一个网页里所有的链接地址提取出来     思路:(1)确定爬取的入口链接               (2)构建提取链接的正则表 ...

  7. Ubuntu上安装使用WeChat、TIM

    WeChat可以直接到软件商店安装,不过是网页版...(其实个人感觉还行,就是什么都不能设置就挺蛋疼的,字体大小.背景什么的) 以下是网上找到的教程,在此总结一下: 下载地址:https://gith ...

  8. Git生成ssh密钥指定文件

    ssh-keygen 使用的时候可以直接使用 -f 参数 指定密钥保存文件,省去后面生成成功后再提示选择保存文件: ssh-keygen -t rsa -C "abc@example.com ...

  9. Html元素添加事件禁用

    最近几天,测试在检测我页面功能时,疯狂点击带接口请求的按钮,然后就会发起无数次请求,然后app就卡住了.当看到这个问题的时候,心里疯狂鄙视测试(开个玩笑),一开始想的到解决方案是用函数防抖,使用函数防 ...

  10. db2数据库常见问题

    db2数据库不能轻易改变表结构,不然表会进入暂挂状态,造成表被锁住. 解锁表语句:call sysproc.admin_cmd('reorg table <table name>');