-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path135.cpp
More file actions
44 lines (44 loc) · 1.1 KB
/
135.cpp
File metadata and controls
44 lines (44 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution
{
public:
int candy(vector<int>& ratings)
{
if(ratings.empty())
return 0;
int size = ratings.size();
int candies = 1;
int sum = 1;
int max = 0;
int downIndex = 0;
for(int i=1;i<size;i++)
{
if(ratings[i]<ratings[i-1])
{
if(downIndex==0)
{
// 记录上一个评分波峰峰值
max = candies;
// 记录评分开始下降的位置
downIndex=i;
}
candies=1;
// 补发糖果
sum+=i-downIndex;
// 判断波峰是否需要补发糖果
if(i-downIndex+1>=max)
sum+=1;
}
else if(ratings[i]>ratings[i-1])
{
downIndex = 0;
candies+=1;
} else {
downIndex = 0;
candies=1;
}
// 发糖果
sum+=candies;
}
return sum;
}
};