-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path135-candy.cpp
52 lines (51 loc) · 1.19 KB
/
135-candy.cpp
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
45
46
47
48
49
50
51
52
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
if (n == 0)
{
return 0;
}
int res = 1;
int pre_n = 0;
int c_pre_n = 1;
int pre = 1;
for (int i = 1; i < n; i++)
{
if (ratings[i] > ratings[i - 1])
{
pre++;
res = res + pre;
pre_n = i;
c_pre_n = pre;
}
else if (ratings[i] == ratings[i - 1])
{
pre = 1;
res = res + pre;
pre_n = i;
c_pre_n = 1;
}
else
{
if (pre == 1)
{
if (i - pre_n <= c_pre_n - 1)
{
res = res + (i - pre_n);
}
else
{
res = res + (i - pre_n + 1);
}
}
else
{
pre = 1;
res = res + 1;
}
}
}
return res;
}
};