-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALGOA_attempt.cpp
55 lines (46 loc) · 1.12 KB
/
ALGOA_attempt.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
//ALGOA try
#include <stdio.h>
#include <limits.h>
#include <bits/stdc++.h>
using namespace std;
int min(int x, int y) { return (x < y)? x: y; }
// Returns minimum number of jumps to reach arr[n-1] from arr[0]
int minJumps(int arr[], int n)
{
int *jumps = new int[n]; // jumps[n-1] will hold the result
int i, j;
if (n == 0 || arr[0] == 0)
return INT_MAX;
jumps[0] = 0;
// Find the minimum number of jumps to reach arr[i]
// from arr[0], and assign this value to jumps[i]
for (i = 1; i < n; i++)
{
jumps[i] = INT_MAX;
for (j = 0; j < i; j++)
{
if (i <= j + arr[j] && jumps[j] != INT_MAX)
{
jumps[i] = min(jumps[i], jumps[j] + 1);
break;
}
}
}
return jumps[n-1];
}
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[1000001] = {0};
for (int i=0; i<n; i++)
{
cin>>a[i];
}
cout<<minJumps(a, n)<<endl;
}
return 0;
}