-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
64 lines (56 loc) · 1.89 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> ones;
for (int i = 0; i < n; ++i) {
int l = (i - 1 < 0) ? numeric_limits<int>::max() : ratings[i - 1];
int r = (i + 1 == n) ? numeric_limits<int>::max() : ratings[i + 1];
if (l >= ratings[i] && ratings[i] <= r) ones.push_back(i);
}
vector<int> candies(n, 1);
for (int m : ones) {
for (int i = 1; m + i < n && ratings[m + i] > ratings[m + i - 1];
++i) {
candies[m + i] = max(candies[m + i], 1 + i);
}
for (int i = 1; m - i >= 0 && ratings[m - i] > ratings[m - i + 1];
++i) {
candies[m - i] = max(candies[m - i], 1 + i);
}
}
return accumulate(candies.begin(), candies.end(), 0);
}
};
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> left(n, 1), right(n, 1);
for (int l = 1, r = n - 2; l < n; ++l, --r) {
if (ratings[l - 1] < ratings[l]) left[l] = left[l - 1] + 1;
if (ratings[r] > ratings[r + 1]) right[r] = right[r + 1] + 1;
}
int result = 0;
for (int i = 0; i < n; ++i) result += max(left[i], right[i]);
return result;
}
};
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> candies(n, 1);
for (int i = 1; i < n; ++i) {
if (ratings[i] > ratings[i - 1]) {
candies[i] = candies[i - 1] + 1;
}
}
for (int i = n - 2; i >= 0; --i) {
if (ratings[i] > ratings[i + 1]) {
candies[i] = max(candies[i], candies[i + 1] + 1);
}
}
return accumulate(candies.begin(), candies.end(), 0);
}
};