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?

左右各遍历一次,依据前一个位置的candy数计算当前位置需要的最小candy数(最少为1)

由于两次遍历的结果都要满足,因此对同一位置取max即可。

class Solution {
public:
int candy(vector<int>& ratings) {
if(ratings.empty())
return ;
int n = ratings.size();
vector<int> left(n, );
for(int i = ; i < n; i ++)
{
if(ratings[i] > ratings[i-])
left[i] = left[i-] + ;
}
vector<int> right(n, );
int sum = max(left[n-], right[n-]);
for(int i = n-; i >= ; i --)
{
if(ratings[i] > ratings[i+])
right[i] = right[i+] + ;
sum += max(left[i], right[i]);
}
return sum;
}
};

【LeetCode】135. Candy的更多相关文章

  1. 【LeetCode】723. Candy Crush 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  2. 【leet-code】135. 加油站

    题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...

  3. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. Go的安装和使用/卸载/升级、安装指定版本

    mac: 1.brew install go默认安装最新的,更新go也是这条命令,建议这个方法,省事 2.从官网https://golang.org/dl/下载pkg包安装,官方说1.12是最后一个支 ...

  2. 关于Java的一些NIO框架的一点想法

    闲着有点无聊想写点东西. 问题:生活中工作中,会有人问我javaNIO框架里面 Netty Mina  xSocket Grizzly 等等哪个比较好? 在这里写一下自己的感受,也算是总结一下吧 在我 ...

  3. 你对position的了解有多少?

    此文根据Steven Bradley的<How Well Do You Understand CSS Positioning?>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之处 ...

  4. MongoDB Sort op eration used more than the maximum 33554432 bytes of RAM. Add an index, or speci fy a smaller limit.

    最近在获取mongodb某个集合的数据过程中,在进行排序的过程中报错,具体报错信息如下: Error: error: { , "errmsg" : "Executor e ...

  5. RichTextBox 清空

    this.tbContent.Document.Blocks.Clear();

  6. 实现ssh的无password登录

    这里所说的ssh是指OpenSSH SSHclient.是用于登录远程主机.而且在远程主机上运行命令.它的目的是替换rlogin和rsh,同一时候在不安全的网络之上,两个互不信任的主机之间,提供加密的 ...

  7. 关于COM的Unicode string的精彩论述

    I need to make a detour for a few moments, and discuss how to handle strings in COM code. If you are ...

  8. JSON字符串转换为Map

    本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: package com.zkn.newlearn.json; import com.alibaba.fastjson.JSO ...

  9. HTTP和Socket的区别

    1: HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用. HTTP ...

  10. 自我分析-Spring IOC

    Spring IOC容器实现原理大致是容器(Map)+反射(Java反射和cglib).Spring提供丰富的ApplicationContext.以FileSystemXmlApplicationC ...