forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjump.cpp
More file actions
34 lines (31 loc) · 742 Bytes
/
jump.cpp
File metadata and controls
34 lines (31 loc) · 742 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
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
class Solution {
public:
int jump(int A[], int n) {
int ret = 0;
int cur = 0, next = 0;
REP(i,n) {
if (i > cur) {
cur = next;
if (next < i) return -1;
++ret;
}
next = max(next, i + A[i]);
}
return ret;
}
};
int main() {
int A[] = {2,3,1,1,4};
int B[] = {2,0,0,0,0};
Solution s;
cout << s.jump(B, 5) << endl;
return 0;
}