forked from Silver-Taurus/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jumpGame.cpp
58 lines (54 loc) · 1.47 KB
/
jumpGame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 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;
}