-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path135.cpp
47 lines (40 loc) · 1.13 KB
/
135.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
#include <vector>
#include <gtest/gtest.h>
using namespace std;
class Solution {
public:
int candy(vector<int>& ratings) {
vector<int> ans (ratings.size(), 1);
for (int i = 1; i < ratings.size(); i++) {
if (ratings[i] > ratings[i - 1]) {
ans[i] = ans[i - 1] + 1;
}
}
for (int i = ratings.size() - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
ans[i] = max(ans[i], ans[i + 1] + 1);
}
}
int sum = 0;
for (int i = 0; i < ans.size(); i++) {
sum += ans[i];
}
return sum;
}
};
class SolutionTest : public ::testing::Test {
protected:
Solution solution;
};
TEST_F(SolutionTest, Test1) {
vector<int> ratings = {1, 0, 2};
EXPECT_EQ(solution.candy(ratings), 5);
}
TEST_F(SolutionTest, Test2) {
vector<int> ratings = {1, 2, 2};
EXPECT_EQ(solution.candy(ratings), 4);
}
int main (int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}