Skip to content

Commit 8ccd66b

Browse files
rakshaa2000Panquesito7kvedala
authored
Created jumpgame.cpp (#1068)
* Created jumpgame.cpp An algorithm to check if you can reach the destination * Changed header files * Changed header files * Fixed warnings * Fixed bug and removed namespace std * fixed bugs final * Updated changes * added documentation Added description of the problem and a brief explanation of the algorithm. * Delete linked list without head pointer You are given a pointer/ reference to the node which is to be deleted from the linked list of N nodes. The task is to delete the node. Head pointer is not given. * fixed bugs * Author_edit_Update greedy_algorithms/jumpgame.cpp Co-authored-by: David Leal <[email protected]> * main_func_Update greedy_algorithms/jumpgame.cpp Co-authored-by: David Leal <[email protected]> * documentation added, testing yet to be added * Added test function * deleted linked list without head * documentation update 1 Co-authored-by: David Leal <[email protected]> * documentation update 2 Co-authored-by: David Leal <[email protected]> * test func update 1 Co-authored-by: David Leal <[email protected]> * documentation updated final * Formatting update Co-authored-by: David Leal <[email protected]> * Formatting update 1 Co-authored-by: David Leal <[email protected]> * Formatting update 2 Co-authored-by: David Leal <[email protected]> * Update return Co-authored-by: David Leal <[email protected]> * Update greedy_algorithms/jumpgame.cpp Co-authored-by: Krishna Vedala <[email protected]> * Update greedy_algorithms/jumpgame.cpp Co-authored-by: Krishna Vedala <[email protected]> * Added link to the problem * documentation update 3 Co-authored-by: David Leal <[email protected]> * documentation update 4 * Update greedy_algorithms/jumpgame.cpp Co-authored-by: David Leal <[email protected]> * Update greedy_algorithms/jumpgame.cpp Co-authored-by: David Leal <[email protected]> * Update greedy_algorithms/jumpgame.cpp Co-authored-by: David Leal <[email protected]> * Revert "Update greedy_algorithms/jumpgame.cpp" This reverts commit 072f9cc. * Revert "Update greedy_algorithms/jumpgame.cpp" This reverts commit f308a5f. * Revert "Update greedy_algorithms/jumpgame.cpp" This reverts commit 4b6eb0c. Co-authored-by: David Leal <[email protected]> Co-authored-by: Krishna Vedala <[email protected]>
1 parent bcf79e2 commit 8ccd66b

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

greedy_algorithms/jumpgame.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @file
3+
* @brief Implementation of an algorithm to solve the [jumping game]((https://leetcode.com/problems/jump-game/)) problem
4+
* @details
5+
* **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.
6+
* This solution takes in input as a vector and output as a boolean to check if you can reach the last position.
7+
* We name the indices good and bad based on whether we can reach the destination if we start at that position.
8+
* We initialize the last index as lastPos.
9+
* Here, we start from the end of the array and check if we can ever reach the first index.
10+
* We check if the sum of the index and the maximum jump count given is greater than or equal to the lastPos.
11+
* If yes, then that is the last position you can reach starting from the back.
12+
* After the end of the loop, if we reach the lastPos as 0, then the destination can be reached from the start position.
13+
* @author [Rakshaa Viswanathan](https://github.com/rakshaa2000)
14+
*/
15+
16+
#include <vector>
17+
#include <iostream>
18+
#include <cassert>
19+
20+
21+
/**
22+
* @brief This function implements the above algorithm
23+
* @param array of numbers containing the maximum jump (in steps) from that index
24+
* @returns bool value whether final index can be reached or not
25+
*/
26+
bool canJump(const std::vector<int> &nums) {
27+
auto lastPos = nums.size() - 1;
28+
for (auto i = nums.size() - 1; i >= 0; i--) {
29+
if (i + nums[i] >= lastPos) {
30+
lastPos = i;
31+
}
32+
}
33+
return lastPos == 0;
34+
}
35+
36+
37+
/**
38+
* @brief Function to test above algorithm
39+
* @returns void
40+
*/
41+
static void test(){
42+
// Test 1
43+
std::vector<int> num1={4,3,1,0,5};
44+
assert(canJump(num1)==true);
45+
std::cout<<"Input: ";
46+
for(auto i: num1){
47+
std::cout<<i<<" ";
48+
}
49+
std::cout<<"Output: true"<<std::endl;
50+
// Test 2
51+
std::vector<int> num2={3,2,1,0,4};
52+
assert(canJump(num2)==false);
53+
std::cout<<"Input: ";
54+
for(auto i: num2){
55+
std::cout<<i<<" ";
56+
}
57+
std::cout<<"Output: false"<<std::endl;
58+
}
59+
60+
61+
/**
62+
* @brief Main function
63+
* @returns 0 on exit
64+
*/
65+
int main(){
66+
test();
67+
return 0;
68+
}

0 commit comments

Comments
 (0)