-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-jumps.js
49 lines (30 loc) · 1.04 KB
/
minimum-jumps.js
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
/*
Given an array of N integers arr[] where each element represents the maximum length of the jump that can be made forward from that element. This means if arr[i] = x, then we can jump any distance y such that y ≤ x.
Find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then you cannot move through that element.
Note: Return -1 if you can't reach the end of the array.
*/
function minJumps(A,n){
//code here
if(n <= 1)
return 0;
if(A[0] == 0)
return -1;
let maxReach = A[0];
let step = A[0];
let jump = 1;
let i = 1;
for(i = 1; i < n; i++){
if(i == n -1)
return jump;
console.log(maxReach, step, i + A[i], i);
maxReach = Math.max(maxReach, i + A[i]);
step--;
if(step == 0){
jump++;
if(i >= maxReach)
return -1;
step = maxReach - i;
}
}
}
console.log(minJumps([9, 10, 1, 2, 3, 4, 8, 0, 0, 0, 0, 0, 0, 0, 1], 15))