果然还是自己的代码有问题。
来回改了好几遍。。
struct CandyNode{
public:
int val;
int rate;
CandyNode *pre, *next;
CandyNode(int r) :val(1),rate(r), pre(NULL), next(NULL){}
};
int candy(vector<int> &ratings) {
int res = 0, n = ratings.size();
if (n == 0)return 0;
int i = 0;
CandyNode *head = new CandyNode(ratings[0]);
CandyNode *p = head, *backp;
for (i = 1; i < ratings.size(); i++){
CandyNode *tmp = new CandyNode(ratings[i]);
p->next = tmp;
tmp->pre = p;
/*if (p->rate == tmp->rate)
{
tmp->val = 1;
}*/
if (p->rate < tmp->rate){
if (p->pre && p->pre->rate >= p->rate)p->val = 1;
backp = p;
while (backp->pre && backp->pre->rate >= backp->rate)
{
if (backp->pre->rate == backp->rate && backp->pre->val < backp->val){
if ((!backp->pre->pre) || (backp->pre->pre && backp->pre->pre->rate < backp->pre->rate))break;
if ((!backp->pre->pre) || (backp->pre->pre && backp->pre->pre->rate >= backp->pre->rate)){
backp->pre->val = 1;
}
else{
backp->pre->val = backp->val;
}
}
else if (backp->pre->rate > backp->rate && backp->pre->val <= backp->val){
backp->pre->val = backp->val + 1;
}
backp = backp->pre;
}
tmp->val = p->val + 1;
}
//last node
if (i == ratings.size() - 1){
backp = tmp;
while (backp->pre && backp->pre->rate >= backp->rate)
{
if (backp->pre->rate == backp->rate && backp->pre->val < backp->val){
if ((!backp->pre->pre) || (backp->pre->pre && backp->pre->pre->rate < backp->pre->rate))break;
backp->pre->val = 1;
}
else if (backp->pre->rate > backp->rate && backp->pre->val <= backp->val){
backp->pre->val = backp->val + 1;
}
backp = backp->pre;
}
if (backp == head){
if (backp->rate > backp->next->rate)backp->val = backp->next->val + 1;
else if (backp->rate == backp->next->rate)backp->val = backp->next->val;
}
}
p = tmp;
}
while (head){
res += head->val;
head = head->next;
}
return res;
}
别人的代码还是那么简单。。。
class Solution {
public:
int candy(vector<int> &ratings) {
int n = ratings.size();
int min_candies = n;
int range = 0;
for (size_t i = 0; i < n - 1; ) {
int start = i;
while (ratings[i] < ratings[i+1] && i < n-1) ++i;
range = i - start;
min_candies += (range * (range + 1)) / 2;
if (i == n-1) break;
start = i;
while (ratings[i] > ratings[i+1] && i < n-1) ++i;
int k = i - start - 1;
min_candies += (k * (k + 1)) / 2;
if (i - start > range) min_candies += (i - start - range);
if (ratings[i] == ratings[i+1]) ++i;
}
return min_candies;
}
};
这个还没仔细看:听说是O(N),空间O(1)
class Solution {
public:
int candy(vector<int> &ratings) {
/*** The solution is O(N) && space O(1) ***/
/*** only one pass for time && only 4 int on stack for storage ***/
int current, peak, back, sum;
if (ratings.empty()) return 0;
current=1;
sum=1;
back=1;
peak=INT_MAX;
for (int i=1;i<ratings.size();i++) {
if (ratings[i]>ratings[i-1]) {
current++;
sum += current;
}
else if (ratings[i]==ratings[i-1]) {
/** Take care of equal case: equal ratings does not need equal candy.
thus, when ratings[i]==ratings[i-1] simply give child i 1 candy. no need
to store i-1's candy as peak because i can go up infinitely without the
necessity to bump i-1 up **/
peak=INT_MAX; // no need to check
current=1;
sum +=current;
back=1;
}
else {
if (current!=1) {// drop to 1
peak=current;
current=1;
sum += current;
back=1;
}
else { // bump all previous (all the way to back's position) up by 1
back++;
sum += back;
if (back==peak) { // can only be true when dropping to 1 happen when ratings[i]<
// ratings[i-1], for equal case, we set peak to INT_MAX to
//prevent it from happening here
sum++; peak++;
}
}
}
}
return sum;
}
};