-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum_jumps.cpp
110 lines (85 loc) · 1.85 KB
/
minimum_jumps.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
int min_jumps_recursive(int *input_arr, int n, int pos) {
if (pos >= n - 1) {
return 0;
}
int curr;
int min = INT_MAX;
for (int i = 1; i <= input_arr[pos]; i++) {
curr = min_jumps_recursive(input_arr, n, pos + i);
if (curr < min) min = curr;
}
return min + 1;
}
int min_jumps_topdown(int *input_arr, int n, int pos, int *dp) {
if (pos >= n - 1) {
return 0;
}
if (dp[pos]) return dp[pos];
int curr;
int min = INT_MAX;
for (int i = 1; i <= input_arr[pos]; i++) {
curr = min_jumps_topdown(input_arr, n, pos + i, dp);
if (curr < min) min = curr;
}
return dp[pos] = min + 1;
}
int min_jumps_bottomup(int *input_arr, int n, int *dp) {
int i;
dp[0] = 0;
for (i = 0; i < n; i++) {
for (int j = 1; j <= input_arr[i] && i + j < n; j++) {
if (dp[i] + 1 < dp[i + j] || dp[i + j] == 0)
dp[i + j] = dp[i] + 1;
}
}
return dp[n - 1];
}
int min_jumps_bottomup_linear(int *input_arr, int n) {
int steps = input_arr[0];
int jumps = 0;
int max_range = 0;
int ind_max_range = 0;
for (int i = 1; i < n; i++) {
if (i + input_arr[i] >= n - 1) {
jumps += 2;
break;
}
steps--;
if (input_arr[i] > max_range) {
max_range = input_arr[i];
ind_max_range = i;
}
if (steps == 0) {
steps = max_range;
max_range = 0;
i = ind_max_range;
jumps++;
}
}
return jumps;
}
void solve() {
int n;
cin >> n;
int input_arr[n];
for (int i = 0; i < n; i++) {
cin >> input_arr[i];
}
//for(auto x : input_arr) cout << x << " "; cout << endl;
int dp[n] = {0};
cout << min_jumps_bottomup_linear(input_arr, n);
}
int main() {
int t;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
clock_t z = clock();
cin >> t;
while (t--) solve();
cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
return 0;
}