【leetcode】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?
本来是抱着找个水题刷一下的态度选择这个题的,但是各种错都郁闷了。。。。(可能也是因为开始的态度就不太对吧)。废话少说,看题:
这个题的意思还真不是很清楚,最开始我的理解是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的更多相关文章
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【leetcode】Candy(python)
题目要求的比它的邻居比自己奖励,因此,我们有最少一个多的.所有我们可以找到所有的坑,凹坑例如,存在以下三种情况. 找到全部的凹点后,我们就能够从凹点处開始向左右两个方向依次查找递增序列.当中每一个高的 ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- Javascript之setTimeout
参考:http://codethoughts.info/javascript/2015/07/06/javascript-callbacks/
- oracle,mybatis主键自增长
<insert id="insert" parameterType="resource"> <selectKey resultType=&qu ...
- Unity3d《Shader篇》地球旋转上空悬浮云层
记得把纹理设置成Repeat Shader "Custom/Earth" { Properties { _MainTex ("Base (RGB)", 2D) ...
- jquery checkbox 复选框多次点击判断选中状态,以及全选/取消的代码示例
2015年12月21日 10:52:51 星期一 目标, 点击当前的checbox, 判断点击后当前checkbox是否是选中状态. html: <input type="checkb ...
- jquery checkbox 限制多选的个数
2015年11月6日 16:32:49 选中第四个的时候提示超过了3个, 点解alert框取消后, 将最后一个选中的checkbox取消选中 <script> $(document).re ...
- java mail api 使用
所需要的jar包: http://pan.baidu.com/s/1qWGZRJm 如果遇到这个错误:在windows防火墙允许 javaw.exe访问网络.或者关闭防火墙 FATAL ERROR i ...
- 内存管理_JAVA内存管理
Java虚拟机规范中试图定义一种Java内存模型(Java Memory Model,JMM)来屏蔽各个硬件平台和操作系统的内存访问差异,以实现让Java程序在各种平台下都能达到一致的内存访问效果.那 ...
- settings的保存位置
xp:C:\Documents and Settings\Administrator\Local Settings\Application Data\ win8 C:\Users\XXX\AppDat ...
- DataStage
parallel job shell调用:dsjob ./dsjob -run -mode NORMAL -paramfile xxx.param <PROJECT> <JOB> ...
- ABAP 内表的行列转换-发货通知单-打印到Excel里
需要传入数据到Excel里的模板如上图所示 ********************** * 设计主要逻辑与原理说明 ...