mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
added documentation
Added description of the problem and a brief explanation of the algorithm.
This commit is contained in:
parent
84766d9443
commit
f3cbd1edc9
@ -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:
|
//Jump Game:
|
||||||
/*Given an array of non-negative integers, you are initially positioned at the first index of the array.
|
/*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.
|
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 <vector>
|
#include <vector>
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
|
|
||||||
|
//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<int> nums) {
|
bool canJump(std::vector<int> nums) {
|
||||||
auto lastPos = nums.size() - 1;
|
auto lastPos = nums.size() - 1;
|
||||||
for (auto i = nums.size() - 1; i >= 0; i--) {
|
for (auto i = nums.size() - 1; i >= 0; i--) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user