-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155_candy.cpp
More file actions
40 lines (35 loc) · 905 Bytes
/
Copy path155_candy.cpp
File metadata and controls
40 lines (35 loc) · 905 Bytes
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
#include <utils.h>
class Solution {
public:
int candy(vector<int>& ratings) {
int size = ratings.size();
if (size < 2) {
return size;
}
vector<int> num(size, 1);
for (int i = 1; i < size; ++i) {
if (ratings[i] > ratings[i-1]) {
num[i] = num[i-1] + 1;
}
}
for (int i = size - 1; i > 0; --i) {
if (ratings[i] < ratings[i-1]) {
num[i-1] = max(num[i-1], num[i] + 1);
}
}
int sum = 0;
for (int i = 0; i < num.size(); i++) {
sum += num[i];
}
return sum;
}
};
TEST(_0155, Candy) {
Solution sln;
vector<int> ratings = {1,0,2};
ASSERT_EQ(sln.candy(ratings), 5);
ratings = {1,2,2};
ASSERT_EQ(sln.candy(ratings), 4);
ratings = {1,3,2,2,1};
ASSERT_EQ(sln.candy(ratings), 7);
}