-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjump-game-v.cpp
65 lines (48 loc) · 1.37 KB
/
jump-game-v.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
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
#include <bits/stdc++.h>
using namespace std;
class Solution {
vector<int> arr;
vector<int> memo;
int d;
int st[1001][11];
int max_query(int L, int R) const {
const int k = 31 - __builtin_clz(R - L + 1);
return max(st[L][k], st[R - (1 << k) + 1][k]);
}
bool can_jump(const int from, const int to) const {
if (to < 0 || to >= (int)arr.size()) return false;
if (arr[from] <= arr[to]) return false;
if (abs(from - to) == 1) return true;
const int highest = max_query(min(from, to) + 1, max(from, to) - 1);
return highest < arr[from];
}
int dp(const int i) {
if (~memo[i]) return memo[i];
int max_value = 0;
for (int j = i - d; j <= i + d; j++) {
if (!can_jump(i, j)) continue;
max_value = max(max_value, dp(j));
}
return memo[i] = 1 + max_value;
}
void build_table() {
const int N = arr.size();
const int size = ceil(log2(N));
for (int i = 0; i < N; i++) st[i][0] = arr[i];
for (int k = 1; k < size; k++)
for (int i = 0; i + (1 << k) <= N; i++)
st[i][k] = max(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]);
}
public:
int maxJumps(vector<int>& arr, int d) {
this->arr = arr;
this->d = d;
build_table();
memo.assign(arr.size(), -1);
int ans = 0;
for (int i = 0; i < (int)arr.size(); i++) {
ans = max(ans, dp(i));
}
return ans;
}
};