Skip to content

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
Co-authored-by: Piotr Idzik <vil02@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 28, 2023
1 parent 9bbb2fd commit 44e4c47
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions greedy_algorithms/jump_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ namespace greedy_algorithms {
* @returns true if the index can be reached
* @returns false if the index can NOT be reached
*/
template <typename T>
bool can_jump(const std::vector<T> &nums) {
bool can_jump(const std::vector<int> &nums) {
size_t lastPos = nums.size() - 1;
for (int i = nums.size() - 1; i >= 0; i--) {
for (size_t i = lastPos; i != static_cast<size_t>(-1); i--) {
if (i + nums[i] >= lastPos) {
lastPos = i;
}
Expand All @@ -56,25 +55,11 @@ bool can_jump(const std::vector<T> &nums) {
* @returns void
*/
static void test() {
// 1st test
std::vector<int> nums = { 4, 3, 1, 0, 5 };
assert(greedy_algorithms::can_jump(nums) == true);

// 2nd test
nums = { 3, 2, 1, 0, 4 };
assert(greedy_algorithms::can_jump(nums) == false);

// 3rd test
nums = { 5, 9, 4, 7, 15, 3 };
assert(greedy_algorithms::can_jump(nums) == true);

// 4th test
nums = { 1, 0, 5, 8, 12 };
assert(greedy_algorithms::can_jump(nums) == false);

// 5th test
nums = {2, 1, 4, 7};
assert(greedy_algorithms::can_jump(nums) == true);
assert(greedy_algorithms::can_jump(std::vector<int>({4, 3, 1, 0, 5})));
assert(!greedy_algorithms::can_jump(std::vector<int>({3, 2, 1, 0, 4})));
assert(greedy_algorithms::can_jump(std::vector<int>({5, 9, 4, 7, 15, 3})));
assert(!greedy_algorithms::can_jump(std::vector<int>({1, 0, 5, 8, 12})));
assert(greedy_algorithms::can_jump(std::vector<int>({2, 1, 4, 7})));

std::cout << "All tests have successfully passed!\n";
}
Expand Down

0 comments on commit 44e4c47

Please sign in to comment.