-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJump Game.cpp
54 lines (49 loc) · 929 Bytes
/
Jump Game.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
#include <iostream>
using namespace std;
class Solution {
public:
bool canJump(int A[], int n) {
int max_jump = A[0];
if(max_jump==0)
if(n==1)
return true;
else
return false;
for(int i=1;i<n;i++){
max_jump = max_jump-1 > A[i]?max_jump-1: A[i];
if(i+max_jump==n-1)
return true;
if(max_jump==0)
return false;
}
return true;
}
};
/*
class Solution {
public:
bool canJump(int A[], int n) {
return help(A,n,0);
}
bool help(int A[], int n, int idx){
if(idx==n)
return true;
int value = A[idx];
if(value==0)
return false;
if(idx + value >= n)
return true;
int temp_i = idx;
for(int i=value;i>temp_i;i--){
if(help(A,n,idx+i))
return true;
}
return false;
}
};
*/
int main(){
Solution s = Solution();
int A[] = {2,3,1,1,4};
cout << s.canJump(A, 5);
}