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 <halfpacho@gmail.com>

* main_func_Update greedy_algorithms/jumpgame.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* documentation added, testing yet to be added

* Added test function

* deleted linked list without head

* documentation update 1

Co-authored-by: David Leal <halfpacho@gmail.com>

* documentation update 2

Co-authored-by: David Leal <halfpacho@gmail.com>

* test func update 1

Co-authored-by: David Leal <halfpacho@gmail.com>

* documentation updated final

* Formatting update

Co-authored-by: David Leal <halfpacho@gmail.com>

* Formatting update 1

Co-authored-by: David Leal <halfpacho@gmail.com>

* Formatting update 2

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update return

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update greedy_algorithms/jumpgame.cpp

Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>

* Update greedy_algorithms/jumpgame.cpp

Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>

* Added link to the problem

* documentation update 3

Co-authored-by: David Leal <halfpacho@gmail.com>

* documentation update 4

* Update greedy_algorithms/jumpgame.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update greedy_algorithms/jumpgame.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update greedy_algorithms/jumpgame.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Revert "Update greedy_algorithms/jumpgame.cpp"

This reverts commit 072f9cc946.

* Revert "Update greedy_algorithms/jumpgame.cpp"

This reverts commit f308a5f6c2.

* Revert "Update greedy_algorithms/jumpgame.cpp"

This reverts commit 4b6eb0cbc9.

Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
Rakshaa Viswanathan 2020-10-02 17:51:16 +05:30 committed by GitHub
parent bcf79e259a
commit 8ccd66bda3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,68 @@
/**
* @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 <vector>
#include <iostream>
#include <cassert>
/**
* @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<int> &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<int> num1={4,3,1,0,5};
assert(canJump(num1)==true);
std::cout<<"Input: ";
for(auto i: num1){
std::cout<<i<<" ";
}
std::cout<<"Output: true"<<std::endl;
// Test 2
std::vector<int> num2={3,2,1,0,4};
assert(canJump(num2)==false);
std::cout<<"Input: ";
for(auto i: num2){
std::cout<<i<<" ";
}
std::cout<<"Output: false"<<std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main(){
test();
return 0;
}