【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-十大经典排序算法
随机推荐
- poj 1011
http://poj.org/problem?id=1011 这是一道POJ的搜索的题目,最开始确实难以理解,但做过一些搜索的题目后,也没那么难了. 大概题意就是,现在有N根木头,要拼成若干根木头,并 ...
- 解读Unity中的CG编写Shader系列六(漫反射)
转自 http://www.itnose.net/detail/6116553.html 如果前面几个系列文章的内容过于冗长缺乏趣味着实见谅,由于时间原因前面的混合部分还没有写完,等以后再补充,现在开 ...
- 解读Unity中的CG编写Shader系列二
转自 http://www.itnose.net/detail/6095974.html 上一篇文章的例子中我们可以看到顶点着色器的输出参数可以说是直接作为了片段着色器的形参传递过来,那么不由得一个问 ...
- ios bitcode 机制对 dsym 调试文件的影响
今天想试试用dsym和crash文件跟踪crash信息,可是一直返回如下信息: Thread name: Dispatch queue: com.apple.main-thread Thread Cr ...
- Sql Server事务简单用法
var conStr = "server=localhost;database=Data;user=sa;pwd=123456"; using (var connection = ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
- BUG归因
文字类1.名称不统一:日期/时间,编号/流水号, 2.单元格式 数据类错误:取值错位 编程上 控件类:JS报错 1.框架收缩 2.置灰,限定修改项 3.隐形,不显示 4.XX报错 5.无法输入:自动补 ...
- 【leetcode】Reverse Linked List(easy)
Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...
- 【leetcode】Convert Sorted Array to Binary Search Tree (easy)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...
- JS调用自定义弹窗【bootstrap】
function commonConfirm(msg,call){ showConfirmBox("消息提示",msg,call); } $(document).on('click ...