-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path135_candy.cc
65 lines (62 loc) · 1.83 KB
/
135_candy.cc
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
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <cstdlib>
#include <iterator>
#include <sched.h>
#include <vector>
#include <iostream>
using namespace::std;
class Solution {
public:
int candy(vector<int>& ratings) {
int sum = 0, prev = 1, peak = 0;
auto size = ratings.size();
if (size == 1)
return 1;
for (unsigned i = 0; i < size; ++i) {
if (i == 0) {
sum += 1;
} else if (i == size - 1) {
if (ratings[i] == ratings[i - 1])
sum += 1;
else
sum += ++prev;
if (peak != 0 && prev >= peak)
sum += prev + 1 - peak;
} else if (ratings[i - 1] == ratings[i] && ratings[i] == ratings[i + 1]) {
sum += 1;
peak = 0;
prev = 1;
} else if (ratings[i - 1] == ratings[i] && ratings[i] != ratings[i + 1]) {
sum += 1;
peak = 0;
prev = 1;
} else if (ratings[i - 1] > ratings[i] && ratings[i] <= ratings[i + 1]) {
sum += ++prev;
if (peak != 0 && prev >= peak)
sum += prev + 1 - peak;
peak = 0;
prev = 1;
} else if (ratings[i - 1] < ratings[i] && ratings[i] > ratings[i + 1]) {
sum += ++prev;
peak = prev;
prev = 0;
} else if (ratings[i - 1] < ratings[i] && ratings[i] == ratings[i + 1]) {
sum += ++prev;
peak = 0;
prev = 1;
} else {
sum += ++prev;
}
}
return sum;
}
};
int main()
{
Solution solu;
vector<int> vec;
int a;
while (cin >> a)
vec.push_back(a);
cout << solu.candy(vec) << endl;
return 0;
}