Skip to content

Commit 44e4c47

Browse files
Panquesito7CaedenPHvil02
committed
chore: apply suggestions from code review
Co-authored-by: Caeden Perelli-Harris <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent 9bbb2fd commit 44e4c47

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

greedy_algorithms/jump_game.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ namespace greedy_algorithms {
3939
* @returns true if the index can be reached
4040
* @returns false if the index can NOT be reached
4141
*/
42-
template <typename T>
43-
bool can_jump(const std::vector<T> &nums) {
42+
bool can_jump(const std::vector<int> &nums) {
4443
size_t lastPos = nums.size() - 1;
45-
for (int i = nums.size() - 1; i >= 0; i--) {
44+
for (size_t i = lastPos; i != static_cast<size_t>(-1); i--) {
4645
if (i + nums[i] >= lastPos) {
4746
lastPos = i;
4847
}
@@ -56,25 +55,11 @@ bool can_jump(const std::vector<T> &nums) {
5655
* @returns void
5756
*/
5857
static void test() {
59-
// 1st test
60-
std::vector<int> nums = { 4, 3, 1, 0, 5 };
61-
assert(greedy_algorithms::can_jump(nums) == true);
62-
63-
// 2nd test
64-
nums = { 3, 2, 1, 0, 4 };
65-
assert(greedy_algorithms::can_jump(nums) == false);
66-
67-
// 3rd test
68-
nums = { 5, 9, 4, 7, 15, 3 };
69-
assert(greedy_algorithms::can_jump(nums) == true);
70-
71-
// 4th test
72-
nums = { 1, 0, 5, 8, 12 };
73-
assert(greedy_algorithms::can_jump(nums) == false);
74-
75-
// 5th test
76-
nums = {2, 1, 4, 7};
77-
assert(greedy_algorithms::can_jump(nums) == true);
58+
assert(greedy_algorithms::can_jump(std::vector<int>({4, 3, 1, 0, 5})));
59+
assert(!greedy_algorithms::can_jump(std::vector<int>({3, 2, 1, 0, 4})));
60+
assert(greedy_algorithms::can_jump(std::vector<int>({5, 9, 4, 7, 15, 3})));
61+
assert(!greedy_algorithms::can_jump(std::vector<int>({1, 0, 5, 8, 12})));
62+
assert(greedy_algorithms::can_jump(std::vector<int>({2, 1, 4, 7})));
7863

7964
std::cout << "All tests have successfully passed!\n";
8065
}

0 commit comments

Comments
 (0)