-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjump.cc
More file actions
95 lines (86 loc) · 2.32 KB
/
jump.cc
File metadata and controls
95 lines (86 loc) · 2.32 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
#define debug_print(...) do {std::cerr<<__func__<<"("<<__LINE__<<"): "<<__VA_ARGS__;} while(0)
class Solution{
public:
bool canJump(int A[], int n)
{
vector<int> v(A,A+n);
vector<int> result(n,-1);
result[0]=1;
canJumpRecursive(v,result,n-1);
return result[n-1];
}
//return whehter point x can be reached, the result will be in result
void canJumpRecursive(const vector<int>& A, vector<int>& result, int x)
{
if (x==0) return;
debug_print("x:"<<x<<endl);
bool search_done=false;
for(int i=x-1; i>=0 && !search_done; i--)
{
if (result[i]==-1)
canJumpRecursive(A,result,i);
if (result[i]==1 && (A[i]+i)>=x)
{
debug_print("judge x:"<<x<<", i:"<<i<<", A[i]:"<<A[i]<<endl);
result[x]=1; //the judgement on x is made
search_done = true;
}
}
if (result[x]==-1)
result[x]=0;
return;
}
int jump(int A[], int n)
{
if (n == 0) return 0;
int *jumpSteps = new int[n];
jumpSteps[0] = 0;
int lastend = 0;
int end, currentJump;
for (int i = 0; i < n - 1; i++)
{
end = i + A[i];
currentJump = jumpSteps[i] + 1;
if (end >= n - 1)
{
end = n - 1;
return currentJump;
}
for (int j = end; j > lastend; j--)
{
debug_print("j:" << j << ", jj:" << jumpSteps[j] << ", le:" << lastend << ", e:" << end << endl);
jumpSteps[j] = currentJump;
}
if (lastend < end) lastend = end;
}
return jumpSteps[n - 1];
}
};
int main()
{
Solution s;
int B[4] = {1,2,3,4};
int A[4] = {1,1,1,1};
int C[]={1,2,1,1,1};
int D[]={3,1,1,1,1,0,0};
int E[]={2,0,0};
//0,1,2,3
//1,2,3,4
//jump 0->1->3
assert(s.jump(B,4)==2);
//0,1,2,3
//1,2,3
//jump 0->1->2
assert(s.jump(B,3)==2);
assert(s.jump(A,4)==3);
assert(s.jump(C,5)==3);
assert(s.jump(D,5)==2);
assert(s.canJump(B,4));
assert(!s.canJump(D,7));
assert(s.canJump(E,3));
return 0;
}