题目描述:

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?

本来是抱着找个水题刷一下的态度选择这个题的,但是各种错都郁闷了。。。。(可能也是因为开始的态度就不太对吧)。废话少说,看题:

这个题的意思还真不是很清楚,最开始我的理解是n个号每个号至少分一个糖,而且排名高的要比排名低的多,觉得就上个排序就了事,nlogn的复杂度应该还是可以接受的吧。于是就开始狂WA了,有以下两个个原因,首先是对rating的理解错误,其实rating应该是数字越小越高,还有一个更加隐蔽的东西,就是它上面的那两个requirement很容易误导我们,第二个其实说的是每个rating高的人必须是比邻居高,也就是说对于和两边相等的我们可以分配一个就行了(这个是过了才领悟到,极其痛苦啊)。

不过后来题目理解完了发现超时了,看来nlogn的算法还是不够优秀,于是就把排序扔了(后来才发现排序本就是错的)。在正确理解题目意思的情况直接扫两遍记录一下就行了。

这里给出几组测试数据,也帮助一下同挂在这个地方的人。

case1:input:[2,3,2]

    output:4

case2:input:[1,2,2,2,3,2,1]

output:11

代码如下:

 class Solution {
public:
int candy(vector<int> &ratings) {
if(ratings.empty()){
return ;
}
if(ratings.size()==){
return ;
}
int l=ratings.size();
int* temp=new int[l];
temp[]=;
for(int i=;i<l;i++){
if(ratings[i]>ratings[i-]){
temp[i]=temp[i-]+;
}
else{
temp[i]=;
}
} int ans=temp[l-];
for(int i=l-;i>=;i--){
if(ratings[i]>ratings[i+]&&temp[i+]+>temp[i]){
temp[i]=temp[i+]+;
}
ans+=temp[i];
}
return ans;
}
};

【leetcode】Candy的更多相关文章

  1. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

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

  2. 【leetcode】Candy(python)

    题目要求的比它的邻居比自己奖励,因此,我们有最少一个多的.所有我们可以找到所有的坑,凹坑例如,存在以下三种情况. 找到全部的凹点后,我们就能够从凹点处開始向左右两个方向依次查找递增序列.当中每一个高的 ...

  3. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  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. 数据库Error:The ScriptCollection in ScriptName not find

    System.InvalidOperationException: The ScriptCollection in ScriptName not find 在 WMI.SQL.HELPER.CONFI ...

  2. Clone Graph

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  3. Wince常见操作

    1.获取本地代码启动路径 //在代码启动路径下存在 Files 文件夹 Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName(). ...

  4. WebViewer报错Error loading document: Invalid XOD file: Zip end header data is wrong size!

    错误:Error loading document: Invalid XOD file: Zip end header data is wrong size! 解决:https://groups.go ...

  5. AC自动机题目汇总

    POJ 4052 ZJU 3430 HDU 4117 HNU 10104 HDU 2457 HNU 11187 ZJU 3545 HDU 3341

  6. Linux下查看某一进程所占用内存的方法

    Linux下查看某一个进程所占用的内存,首先可以通过ps命令找到进程id,比如 ps -ef | grep kafka 可以看到kafka这个程序的进程id 可以看到是2913,现在可以使用如下命令查 ...

  7. java 入门 第二季3

    1.继承 1.java是单继承的,一个子类只有一个父类 父类,基类 子类,派生类 2.继承的好处:子类拥有父类的所有属性和方法,属性的修饰符不能是private 3.语法规则: class 子类 ex ...

  8. Django~urls.py--->views.py

    The 'r' in front of each regular expression string is optional but recommended. It tells Python that ...

  9. [第三方]SCNetworkReachability 获取网络状态控件使用方法

    用Cocoa Pods导入控件以后 直接导头文件 复制以下代码 [SCNetworkReachability host:@"github.com" reachabilityStat ...

  10. ZOJ 3812 We Need Medicine(dp、背包、状态压缩、路径记录)

    参考:http://blog.csdn.net/qian99/article/details/39138329 参考的链接里说明得很好,注释也很好...thanks for sharing 朴素的想法 ...