Jump Game Algorithm
The Jump Game II Algorithm is an optimization-based algorithm that solves the classic "jump game" problem. In this problem, a player is given an array of non-negative integers, where each integer represents the maximum jump length at that position. The player's goal is to reach the last index in the minimum number of jumps. The algorithm works by utilizing a greedy approach, focusing on finding the farthest reachable index within the current jump range and updating the jump range as the player moves forward in the array.
The key to the Jump Game II Algorithm is to keep track of the current jump range, the farthest reachable index, and the number of jumps required to reach the last index. The algorithm iterates through the input array, updating the farthest reachable index by comparing the current maximum reachable index and the sum of the current index and its corresponding jump length. When the current index reaches the end of the current jump range, the jump range is updated to the farthest reachable index, and the number of jumps is incremented. This process continues until the last index is reached, at which point the algorithm returns the minimum number of jumps required. By focusing on the farthest reachable index and only updating the jump range when necessary, the Jump Game II Algorithm efficiently finds the minimum number of jumps needed to reach the end 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.
*
* Determine if you are able to reach the last index.
*
* For example:
* A = [2,3,1,1,4], return true.
*
* A = [3,2,1,0,4], return false.
*
* Approach : Here we have to observe that array value represents *maximum* value, so we can always jump j index from index i as long as j < A[i].
* i.e. if A[i] = 3, we can jump either 1, 2, or 3 steps.
* Now, We will use dynamic programing to solve this problem. Our subproblem is at any index i, what is the farthest index I can jump. If in the end
* if the max value index we have gathered so far is bigger than array length, we have reached the last index.
*/
#include <iostream>
#include <vector>
bool canReach( std::vector<int> & vec ) {
int n = vec.size();
if ( n == 0 ) {
return false;
}
if ( n == 1 ) {
return true;
}
int m = 0;
for (int i = 0; i < n; ++i ) {
if ( i <= m ) {
m = std::max( m, vec[i] + i );
if ( m >= n-1) {
return true;
}
}
}
return false;
}
void printRes( std::vector<int> & vec ) {
std::cout << "VEC:";
for ( auto v : vec ) {
std::cout << v << " ";
}
std::cout << std::endl;
std::cout << (canReach(vec) ? "True" : "False");
std::cout << std::endl;
}
int main() {
std::vector<int> vec1{ 2,3,1,1,4 };
std::vector<int> vec2{ 3,2,1,0,4 };
printRes(vec1);
printRes(vec2);
return 0;
}