-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55.cpp
115 lines (103 loc) · 2.36 KB
/
55.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <algorithm>
#include <cassert>
#include <gtest/gtest.h>
#include <vector>
using namespace std;
class Solution {
public:
bool canJump(vector<int> &nums)
{
if (nums.size() == 1) {
return true;
}
if (nums[0] == 0) {
return false;
}
if (find(nums.begin(), nums.end() - 1, 0) == nums.end() - 1) {
return true;
}
int evidence = 0;
for (int i = 0; i < nums.size() - 1; i++) {
if (nums[i] == 0) {
evidence = 0;
int j = i - 1;
while (j >= 0) {
if (nums[j] > i - j) {
evidence++;
}
j--;
}
if (evidence == 0) {
return false;
}
}
}
return true;
}
};
class Testing : public testing::Test {
public:
Solution solution;
void SetUp() {}
void TearDown() {}
};
TEST_F(Testing, Case1)
{
vector<int> nums = {2, 3, 1, 1, 4};
bool expected = true;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case2)
{
vector<int> nums = {3, 2, 1, 0, 4};
bool expected = false;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case3)
{
vector<int> nums = {0};
bool expected = true;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case4)
{
vector<int> nums = {1, 0, 1, 0};
bool expected = false;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case5)
{
vector<int> nums = {1, 1, 1, 1};
bool expected = true;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case6)
{
vector<int> nums = {1, 1, 1, 1, 0};
bool expected = true;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case7)
{
vector<int> nums = {0, 1};
bool expected = false;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case8)
{
vector<int> nums = {1, 1, 1, 0};
bool expected = true;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case9)
{
vector<int> nums = {2, 0, 1, 0, 1};
bool expected = false;
EXPECT_EQ(solution.canJump(nums), expected);
}
TEST_F(Testing, Case10)
{
vector<int> nums = {2, 0, 0};
bool expected = true;
EXPECT_EQ(solution.canJump(nums), expected);
}