From 0f2ce72be4fe35a029c2dea8c8facaead229c8a8 Mon Sep 17 00:00:00 2001 From: Rakshaa Viswanathan <46165429+rakshaa2000@users.noreply.github.com> Date: Tue, 1 Sep 2020 00:04:11 +0530 Subject: [PATCH 1/8] Created jumpgame.cpp An algorithm to check if you can reach the destination --- greedy_algorithms/jumpgame.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 greedy_algorithms/jumpgame.cpp diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp new file mode 100644 index 000000000..f61e93d13 --- /dev/null +++ b/greedy_algorithms/jumpgame.cpp @@ -0,0 +1,21 @@ +//Jump Game: +/*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.*/ + +#include +bool canJump(vector& nums) { + int lastPos = nums.size() - 1; + for (int i = nums.size() - 1; i >= 0; i--) { + if (i + nums[i] >= lastPos) { + lastPos = i; + } + } + return lastPos == 0; +} + +void main(){ + //Sample test case + vector num={4,3,1,0,5}; + cout< Date: Tue, 1 Sep 2020 00:08:54 +0530 Subject: [PATCH 2/8] Changed header files --- greedy_algorithms/jumpgame.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index f61e93d13..2c33b9141 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -3,7 +3,9 @@ Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index.*/ -#include +#include +#include +using namespace std; bool canJump(vector& nums) { int lastPos = nums.size() - 1; for (int i = nums.size() - 1; i >= 0; i--) { From e845e4860d8e6fbee2b12b82b5200b5e7b50e3d2 Mon Sep 17 00:00:00 2001 From: Rakshaa Viswanathan <46165429+rakshaa2000@users.noreply.github.com> Date: Tue, 1 Sep 2020 00:09:11 +0530 Subject: [PATCH 3/8] Changed header files From 35c99662ed54a6de2ede75ad2b77801a90f0364c Mon Sep 17 00:00:00 2001 From: Rakshaa Viswanathan <46165429+rakshaa2000@users.noreply.github.com> Date: Tue, 1 Sep 2020 00:12:43 +0530 Subject: [PATCH 4/8] Fixed warnings --- greedy_algorithms/jumpgame.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index 2c33b9141..25c6f1c05 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -6,9 +6,9 @@ Determine if you are able to reach the last index.*/ #include #include using namespace std; -bool canJump(vector& nums) { +bool canJump(vector nums) { int lastPos = nums.size() - 1; - for (int i = nums.size() - 1; i >= 0; i--) { + for (auto i = nums.size() - 1; i >= 0; i--) { if (i + nums[i] >= lastPos) { lastPos = i; } From 331638065c31aaa8b36613352ffd30072b694836 Mon Sep 17 00:00:00 2001 From: Rakshaa Viswanathan <46165429+rakshaa2000@users.noreply.github.com> Date: Tue, 1 Sep 2020 00:15:34 +0530 Subject: [PATCH 5/8] Fixed bug and removed namespace std --- greedy_algorithms/jumpgame.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index 25c6f1c05..60b7edd35 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -5,9 +5,9 @@ Determine if you are able to reach the last index.*/ #include #include -using namespace std; + bool canJump(vector nums) { - int lastPos = nums.size() - 1; + auto lastPos = nums.size() - 1; for (auto i = nums.size() - 1; i >= 0; i--) { if (i + nums[i] >= lastPos) { lastPos = i; From 8622683556a05d935ff6cf2f88f286f80584d78a Mon Sep 17 00:00:00 2001 From: Rakshaa Viswanathan <46165429+rakshaa2000@users.noreply.github.com> Date: Tue, 1 Sep 2020 00:18:51 +0530 Subject: [PATCH 6/8] fixed bugs final --- greedy_algorithms/jumpgame.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index 60b7edd35..7699abdc7 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -6,7 +6,7 @@ Determine if you are able to reach the last index.*/ #include #include -bool canJump(vector nums) { +bool canJump(std::vector nums) { auto lastPos = nums.size() - 1; for (auto i = nums.size() - 1; i >= 0; i--) { if (i + nums[i] >= lastPos) { @@ -16,8 +16,9 @@ bool canJump(vector nums) { return lastPos == 0; } -void main(){ +int main(){ //Sample test case - vector num={4,3,1,0,5}; + std::vector num={4,3,1,0,5}; cout< Date: Tue, 1 Sep 2020 00:22:20 +0530 Subject: [PATCH 7/8] Updated changes --- greedy_algorithms/jumpgame.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index 7699abdc7..64b82c1a9 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -19,6 +19,6 @@ bool canJump(std::vector nums) { int main(){ //Sample test case std::vector num={4,3,1,0,5}; - cout< Date: Tue, 1 Sep 2020 09:55:04 +0530 Subject: [PATCH 8/8] added documentation Added description of the problem and a brief explanation of the algorithm. --- greedy_algorithms/jumpgame.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index 64b82c1a9..fbe4020c4 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -1,3 +1,11 @@ +/** + * @file + * \brief Implementation of an algorithm to solve the jumping game problem + * \details + * This algorithm is a greedy algorithm. + * This solution takes in input as a vector and output as a boolean to check if you can reach the last position. + * @author rakshaa2000 +*/ //Jump Game: /*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. @@ -6,6 +14,14 @@ Determine if you are able to reach the last index.*/ #include #include +//Implements the algorithm +/*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. +*/ bool canJump(std::vector nums) { auto lastPos = nums.size() - 1; for (auto i = nums.size() - 1; i >= 0; i--) {