-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dp_n.cpp
39 lines (34 loc) · 838 Bytes
/
AC_dp_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dp_n.cpp
* Create Date: 2015-01-30 09:46:40
* Descripton: f[i] means the max steps can go at i
* f[i] = max(f[i - 1], A[i - 1]) - 1
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
bool canJump(int A[], int n) {
// vector<int> f(n, 0);
int f[2]; // compress to O(1)
f[0] = 0;
for (int i = 1; i < n; ++i) {
f[i&1] = max(f[(i-1)&1], A[i - 1]) - 1;
if (f[i&1] < 0)
return false;
}
return f[(n-1)&1] >= 0;
}
};
int main() {
int n, A[1000];
Solution s;
while (cin >> n) {
for (int i = 0; i < n; i++)
cin >> A[i];
cout << s.canJump(A, n) << endl;
}
return 0;
}