-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminJumpDp.cpp
More file actions
44 lines (33 loc) · 765 Bytes
/
minJumpDp.cpp
File metadata and controls
44 lines (33 loc) · 765 Bytes
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
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
void reconstructPath(int start, int end, vector<int> & p){
if (start == end) {
cout << start ;
return;
}
reconstructPath(start, p[end], p);
cout<<" -> "<<end;
}
int findMinimumJumps( const vector<int> & v){
vector<int> dp(v.size(), INT_MAX);
vector<int> p(v.size(),0);
dp[0] = 0; p[0] = 0;
for (int i = 1; i < v.size(); ++i){
for (int j = 0; j < i; ++j) {
if (j+v[j] >= i) {
if (dp[j]+1 < dp[i]) {
dp[i] = dp[j]+1;
p[i] = j;
}
}
}
}
reconstructPath(0, v.size()-1, p);
return dp[v.size()-1];
}
int main() {
vector<int> v = { 2,3,1,1,2,4,2,0,1,1};
cout<<endl<<findMinimumJumps(v)<<endl;
}