-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_jumps.cpp
More file actions
54 lines (53 loc) · 1.29 KB
/
minimum_jumps.cpp
File metadata and controls
54 lines (53 loc) · 1.29 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
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline istream &operator>>(istream &in, vector<T> &v) {
for (T &x : v) {
in >> x;
}
return in;
}
template <class T> inline ostream &operator<<(ostream &out, vector<T> &v) {
if (v.empty())
return out;
for (size_t i = 0; i < v.size() - 1; i++) {
out << v[i] << ' ';
}
out << v[v.size() - 1];
return out;
}
class Solution {
public:
int minJumps(vector<int> &arr) {
int n = arr.size();
vector<int> helper(n, 0);
for (int i = 0; i < n; i++) {
cout<<helper<<endl;
if (helper[min(arr[i] + i + 1, n)-1]!=0) {
continue;
}
for (int j = i; j < min(arr[i] + i + 1, n); j++) {
if (helper[j] == 0) {
helper[j] = helper[i] + 1;
}
}
if (i + 1 < n and helper[i + 1] == 0) {
break;
}
}
cout<<helper<<endl;
if (helper[n - 1] == 0) {
return -1;
}
return helper[n - 1] - 1;
}
};
signed main() {
Solution s;
vector<int> v{1, 2, 0, 0, 0};
cout << s.minJumps(v);
return 0;
}