【LeetCode笔记】Candy
2014-08-28 14:40
211 查看
Something wrong with my solution as describing below.
I got a wrong answer: Input: [1,2,4,4,3] Output: 10 Expected: 9
my output is 1+2+3+3+1 = 10. and I think OJ's is 1+2+3+2+1 = 9
how can the third child has more candies than the fourth?
But the requirement is "Children with a higher rating get more candies than their neighbors",
I don't want to deal with my code anymore, because I think this question is not well defined.
------------------------------------------------------------------------------
I found a nice code in the discussion:
ref: https://oj.leetcode.com/discuss/76/does-anyone-have-a-better-idea
Here is another wonderful solution from old discuss by hawk. Thanks to hawk!
The solution is O(N) time complexity and constant memory complexity. What's more, the solution only need to go thru the ratings array once!
Reading the code with full comment to help understand the algorithm.
int candy(vector<int> &ratings) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int nCandyCnt = 0;///Total candies
int nSeqLen = 0; /// Continuous ratings descending sequence length
int nPreCanCnt = 1; /// Previous child's candy count
int nMaxCntInSeq = nPreCanCnt;
if(ratings.begin() != ratings.end())
{
nCandyCnt++;//Counting the first child's candy.
for(vector<int>::iterator i = ratings.begin()+1; i!= ratings.end(); i++)
{
// if r[k]>r[k+1]>r[k+2]...>r[k+n],r[k+n]<=r[k+n+1],
// r[i] needs n-(i-k)+(Pre's) candies(k<i<k+n)
// But if possible, we can allocate one candy to the child,
// and with the sequence extends, add the child's candy by one
// until the child's candy reaches that of the prev's.
// Then increase the pre's candy as well.
// if r[k] < r[k+1], r[k+1] needs one more candy than r[k]
//
if(*i < *(i-1))
{
//Now we are in a sequence
nSeqLen++;
if(nMaxCntInSeq == nSeqLen)
{
//The first child in the sequence has the same candy as the prev
//The prev should be included in the sequence.
nSeqLen++;
}
nCandyCnt+= nSeqLen;
nPreCanCnt = 1;
}
else
{
if(*i > *(i-1))
{
nPreCanCnt++;
}
else
{
nPreCanCnt = 1;
}
nCandyCnt += nPreCanCnt;
nSeqLen = 0;
nMaxCntInSeq = nPreCanCnt;
}
}
}
return nCandyCnt;
}
I got a wrong answer: Input: [1,2,4,4,3] Output: 10 Expected: 9
my output is 1+2+3+3+1 = 10. and I think OJ's is 1+2+3+2+1 = 9
how can the third child has more candies than the fourth?
But the requirement is "Children with a higher rating get more candies than their neighbors",
I don't want to deal with my code anymore, because I think this question is not well defined.
------------------------------------------------------------------------------
I found a nice code in the discussion:
ref: https://oj.leetcode.com/discuss/76/does-anyone-have-a-better-idea
Here is another wonderful solution from old discuss by hawk. Thanks to hawk!
The solution is O(N) time complexity and constant memory complexity. What's more, the solution only need to go thru the ratings array once!
Reading the code with full comment to help understand the algorithm.
int candy(vector<int> &ratings) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int nCandyCnt = 0;///Total candies
int nSeqLen = 0; /// Continuous ratings descending sequence length
int nPreCanCnt = 1; /// Previous child's candy count
int nMaxCntInSeq = nPreCanCnt;
if(ratings.begin() != ratings.end())
{
nCandyCnt++;//Counting the first child's candy.
for(vector<int>::iterator i = ratings.begin()+1; i!= ratings.end(); i++)
{
// if r[k]>r[k+1]>r[k+2]...>r[k+n],r[k+n]<=r[k+n+1],
// r[i] needs n-(i-k)+(Pre's) candies(k<i<k+n)
// But if possible, we can allocate one candy to the child,
// and with the sequence extends, add the child's candy by one
// until the child's candy reaches that of the prev's.
// Then increase the pre's candy as well.
// if r[k] < r[k+1], r[k+1] needs one more candy than r[k]
//
if(*i < *(i-1))
{
//Now we are in a sequence
nSeqLen++;
if(nMaxCntInSeq == nSeqLen)
{
//The first child in the sequence has the same candy as the prev
//The prev should be included in the sequence.
nSeqLen++;
}
nCandyCnt+= nSeqLen;
nPreCanCnt = 1;
}
else
{
if(*i > *(i-1))
{
nPreCanCnt++;
}
else
{
nPreCanCnt = 1;
}
nCandyCnt += nPreCanCnt;
nSeqLen = 0;
nMaxCntInSeq = nPreCanCnt;
}
}
}
return nCandyCnt;
}
相关文章推荐
- LeetCode 笔记25 Candy (艰难的调试)
- LeetCode 笔记系列二 Container With Most Water
- Candy研究笔记-xmpp
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
- 【LeetCode】Reverse Integer解题笔记
- LeetCode 笔记系列 19 Scramble String [合理使用递归]
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
- LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
- LeetCode题目笔记(一) -- Minimum Depth Of Binary Tree
- LeetCode 笔记系列五 Generate Parentheses
- LeetCode 笔记系列九 Search in Rotated Sorted Array
- LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]
- LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]
- LeetCode题目笔记(二) -- Maximum Depth of Binary Tree
- LeetCode题目笔记(四) -- Check if a binary tree is balanced
- LeetCode 笔记系列四 Remove Nth Node From End of List
- LeetCode 笔记系列 14 N-Queen II [思考的深度问题]
- [LeetCode]Best Time to Buy and Sell Stock做题笔记