-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion64.c
60 lines (49 loc) · 844 Bytes
/
question64.c
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
/*
http://practice.geeksforgeeks.org/problems/minimum-number-of-jumps/0
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int minJumps(int *arr, int size) {
int i;
int ref[size];
for(i=0; i<size;i++) {
ref[i] = 1;
}
ref[size-1] = 0;
for(i=size-2;i>=0;i--) {
int value = arr[i];
int j = i;
int min = 2000;
while(j < size && value) {
j++;
if(min > ref[j]) {
min = ref[j];
}
value--;
}
// printf("value of min is %d\n", min);
ref[i] += min;
// printf("ref[%d] is %d\n",i, ref[i]);
}
if(ref[0] >= 2000) {
return -1;
}
return ref[0];
}
int main() {
int cases;
int i;
scanf("%d",&cases);
for(i=0;i<cases;i++){
int size;
scanf("%d",&size);
int arr[size];
int j;
for(j=0;j<size;j++){
scanf("%d",&arr[j]);
}
printf("%d\n", minJumps(arr, size));
}
return 0;
}