From 037b28b287b2c70922faa376c139764495932b4f Mon Sep 17 00:00:00 2001 From: David Leal Date: Wed, 26 Jul 2023 01:15:43 +0000 Subject: [PATCH 1/4] [fix/docs]: improve the Jump Game algorithm --- greedy_algorithms/jump_game.cpp | 87 +++++++++++++++++++++++++++++++++ greedy_algorithms/jumpgame.cpp | 68 -------------------------- 2 files changed, 87 insertions(+), 68 deletions(-) create mode 100644 greedy_algorithms/jump_game.cpp delete mode 100644 greedy_algorithms/jumpgame.cpp diff --git a/greedy_algorithms/jump_game.cpp b/greedy_algorithms/jump_game.cpp new file mode 100644 index 00000000000..1a9a04bc359 --- /dev/null +++ b/greedy_algorithms/jump_game.cpp @@ -0,0 +1,87 @@ +/** + * @file + * @brief [Jumping Game](https://leetcode.com/problems/jump-game/) + * algorithm implementation + * @details + * + * Given an array of non-negative integers, you are initially positioned at the + * first index of the array. Each element in the array represents your maximum + * jump length at that position. Determine if you are able to reach the last + * index. This solution takes in input as a vector and output as a boolean to + * check if you can reach the last position. We name the indices good and bad + * based on whether we can reach the destination if we start at that position. + * We initialize the last index as lastPos. + * Here, we start from the end of the array and check if we can ever reach the + * first index. We check if the sum of the index and the maximum jump count + * given is greater than or equal to the lastPos. If yes, then that is the last + * position you can reach starting from the back. After the end of the loop, if + * we reach the lastPos as 0, then the destination can be reached from the start + * position. + * + * @author [Rakshaa Viswanathan](https://github.com/rakshaa2000) + * @author [David Leal](https://github.com/Panquesito7) + */ + +#include /// for assert +#include /// for std::cout +#include /// for std::vector + +/** + * @namespace + * @brief Greedy Algorithms + */ +namespace greedy_algorithms { +/** + * @brief Checks whether the given element (default is `1`) can jump to the last + * index. + * @param nums array of numbers containing the maximum jump (in steps) from that + * index + * @returns true if the index can be reached + * @returns false if the index can NOT be reached + */ +template +bool can_jump(const std::vector &nums, int index = 1) { + const int size = nums.size() + 1 - nums[index]; + if (nums[index] >= size) { // `>=` because the number can be higher than the size of the array. + return true; + } + return false; +} +} // namespace greedy_algorithms + +/** + * @brief Function to test above algorithm + * @returns void + */ +static void test() { + // 1st test + std::vector 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 = { 4, 2, 8, 9, 6 }; + assert(greedy_algorithms::can_jump(nums) == false); + + // 5th test + nums = {7, 4, 8, 13, 2, 11}; + assert(greedy_algorithms::can_jump(nums) == true); + + std::cout << "All tests have successfully passed!\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +} diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp deleted file mode 100644 index 8a890319154..00000000000 --- a/greedy_algorithms/jumpgame.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/** - * @file - * @brief Implementation of an algorithm to solve the [jumping game]((https://leetcode.com/problems/jump-game/)) problem - * @details - * **Problem statement:** Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. - * This solution takes in input as a vector and output as a boolean to check if you can reach the last position. - * We name the indices good and bad based on whether we can reach the destination if we start at that position. - * We initialize the last index as lastPos. - * Here, we start from the end of the array and check if we can ever reach the first index. - * We check if the sum of the index and the maximum jump count given is greater than or equal to the lastPos. - * If yes, then that is the last position you can reach starting from the back. - * After the end of the loop, if we reach the lastPos as 0, then the destination can be reached from the start position. - * @author [Rakshaa Viswanathan](https://github.com/rakshaa2000) -*/ - -#include -#include -#include - - - /** - * @brief This function implements the above algorithm - * @param array of numbers containing the maximum jump (in steps) from that index - * @returns bool value whether final index can be reached or not - */ -bool canJump(const std::vector &nums) { - auto lastPos = nums.size() - 1; - for (auto i = nums.size() - 1; i >= 0; i--) { - if (i + nums[i] >= lastPos) { - lastPos = i; - } - } - return lastPos == 0; -} - - -/** - * @brief Function to test above algorithm - * @returns void - */ -static void test(){ - // Test 1 - std::vector num1={4,3,1,0,5}; - assert(canJump(num1)==true); - std::cout<<"Input: "; - for(auto i: num1){ - std::cout< num2={3,2,1,0,4}; - assert(canJump(num2)==false); - std::cout<<"Input: "; - for(auto i: num2){ - std::cout< Date: Tue, 25 Jul 2023 20:25:37 -0600 Subject: [PATCH 2/4] fix: pass `index` by reference and `const` it --- greedy_algorithms/jump_game.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/greedy_algorithms/jump_game.cpp b/greedy_algorithms/jump_game.cpp index 1a9a04bc359..7ecb0296dd0 100644 --- a/greedy_algorithms/jump_game.cpp +++ b/greedy_algorithms/jump_game.cpp @@ -40,7 +40,7 @@ namespace greedy_algorithms { * @returns false if the index can NOT be reached */ template -bool can_jump(const std::vector &nums, int index = 1) { +bool can_jump(const std::vector &nums, const int &index = 1) { const int size = nums.size() + 1 - nums[index]; if (nums[index] >= size) { // `>=` because the number can be higher than the size of the array. return true; @@ -50,7 +50,7 @@ bool can_jump(const std::vector &nums, int index = 1) { } // namespace greedy_algorithms /** - * @brief Function to test above algorithm + * @brief Function to test the above algorithm * @returns void */ static void test() { @@ -71,7 +71,7 @@ static void test() { assert(greedy_algorithms::can_jump(nums) == false); // 5th test - nums = {7, 4, 8, 13, 2, 11}; + nums = { 7, 4, 8, 13, 2, 11 }; assert(greedy_algorithms::can_jump(nums) == true); std::cout << "All tests have successfully passed!\n"; From 9bbb2fda817c13f3e509576b1e1db0092a37c1f3 Mon Sep 17 00:00:00 2001 From: David Leal Date: Wed, 26 Jul 2023 18:40:34 +0000 Subject: [PATCH 3/4] fix: use previous algorithm --- greedy_algorithms/jump_game.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/greedy_algorithms/jump_game.cpp b/greedy_algorithms/jump_game.cpp index 7ecb0296dd0..de1c5091a7b 100644 --- a/greedy_algorithms/jump_game.cpp +++ b/greedy_algorithms/jump_game.cpp @@ -40,12 +40,14 @@ namespace greedy_algorithms { * @returns false if the index can NOT be reached */ template -bool can_jump(const std::vector &nums, const int &index = 1) { - const int size = nums.size() + 1 - nums[index]; - if (nums[index] >= size) { // `>=` because the number can be higher than the size of the array. - return true; +bool can_jump(const std::vector &nums) { + size_t lastPos = nums.size() - 1; + for (int i = nums.size() - 1; i >= 0; i--) { + if (i + nums[i] >= lastPos) { + lastPos = i; + } } - return false; + return lastPos == 0; } } // namespace greedy_algorithms @@ -67,11 +69,11 @@ static void test() { assert(greedy_algorithms::can_jump(nums) == true); // 4th test - nums = { 4, 2, 8, 9, 6 }; + nums = { 1, 0, 5, 8, 12 }; assert(greedy_algorithms::can_jump(nums) == false); // 5th test - nums = { 7, 4, 8, 13, 2, 11 }; + nums = {2, 1, 4, 7}; assert(greedy_algorithms::can_jump(nums) == true); std::cout << "All tests have successfully passed!\n"; From 44e4c4716fb5fc3300a3cdc849f66c505a3aa136 Mon Sep 17 00:00:00 2001 From: David Leal Date: Fri, 28 Jul 2023 08:01:38 +0000 Subject: [PATCH 4/4] chore: apply suggestions from code review Co-authored-by: Caeden Perelli-Harris Co-authored-by: Piotr Idzik --- greedy_algorithms/jump_game.cpp | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/greedy_algorithms/jump_game.cpp b/greedy_algorithms/jump_game.cpp index de1c5091a7b..4bf126bc783 100644 --- a/greedy_algorithms/jump_game.cpp +++ b/greedy_algorithms/jump_game.cpp @@ -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 -bool can_jump(const std::vector &nums) { +bool can_jump(const std::vector &nums) { size_t lastPos = nums.size() - 1; - for (int i = nums.size() - 1; i >= 0; i--) { + for (size_t i = lastPos; i != static_cast(-1); i--) { if (i + nums[i] >= lastPos) { lastPos = i; } @@ -56,25 +55,11 @@ bool can_jump(const std::vector &nums) { * @returns void */ static void test() { - // 1st test - std::vector 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({4, 3, 1, 0, 5}))); + assert(!greedy_algorithms::can_jump(std::vector({3, 2, 1, 0, 4}))); + assert(greedy_algorithms::can_jump(std::vector({5, 9, 4, 7, 15, 3}))); + assert(!greedy_algorithms::can_jump(std::vector({1, 0, 5, 8, 12}))); + assert(greedy_algorithms::can_jump(std::vector({2, 1, 4, 7}))); std::cout << "All tests have successfully passed!\n"; }