2020-09-01 09:55:04 +05:30
|
|
|
/**
|
|
|
|
* @file
|
2020-09-05 10:29:59 +05:30
|
|
|
* @brief Implementation of an algorithm to solve the jumping game problem
|
|
|
|
* @details
|
2020-09-01 09:55:04 +05:30
|
|
|
* 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.
|
2020-09-05 10:29:59 +05:30
|
|
|
* 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.
|
2020-09-02 22:42:31 +05:30
|
|
|
* @author [Rakshaa Viswanathan](https://github.com/rakshaa2000)
|
2020-09-01 09:55:04 +05:30
|
|
|
*/
|
2020-09-01 00:04:11 +05:30
|
|
|
|
2020-09-01 00:08:54 +05:30
|
|
|
#include <vector>
|
2020-09-06 09:49:00 +05:30
|
|
|
#include <iostream>
|
2020-09-04 11:26:42 +05:30
|
|
|
#include <cassert>
|
2020-09-01 00:15:34 +05:30
|
|
|
|
2020-09-05 10:29:59 +05:30
|
|
|
|
2020-09-04 11:16:15 +05:30
|
|
|
/**
|
2020-09-04 11:26:42 +05:30
|
|
|
* @brief This function implements the above algorithm
|
2020-09-05 10:24:12 +05:30
|
|
|
* @param array of numbers containing the maximum jump (in steps) from that index
|
2020-09-04 11:16:15 +05:30
|
|
|
* @return returns bool value whether final index can be reached or not
|
|
|
|
*/
|
2020-09-01 00:18:51 +05:30
|
|
|
bool canJump(std::vector<int> nums) {
|
2020-09-01 00:15:34 +05:30
|
|
|
auto lastPos = nums.size() - 1;
|
2020-09-01 00:12:43 +05:30
|
|
|
for (auto i = nums.size() - 1; i >= 0; i--) {
|
2020-09-01 00:04:11 +05:30
|
|
|
if (i + nums[i] >= lastPos) {
|
|
|
|
lastPos = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lastPos == 0;
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:26:42 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Function to test above algorithm
|
2020-09-05 10:25:47 +05:30
|
|
|
* @returns void
|
2020-09-04 11:26:42 +05:30
|
|
|
*/
|
2020-09-05 10:26:27 +05:30
|
|
|
static void test(){
|
2020-09-04 11:26:42 +05:30
|
|
|
//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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-02 22:43:02 +05:30
|
|
|
/**
|
|
|
|
* @brief Main function
|
|
|
|
* @returns 0 on exit
|
|
|
|
*/
|
2020-09-01 00:18:51 +05:30
|
|
|
int main(){
|
2020-09-04 11:26:42 +05:30
|
|
|
test();
|
2020-09-01 00:18:51 +05:30
|
|
|
return 0;
|
2020-09-01 00:04:11 +05:30
|
|
|
}
|