-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMinimumNumberOfJumps.js
47 lines (44 loc) · 1.2 KB
/
MinimumNumberOfJumps.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
/**
* @author Anirudh Sharma
*
* Given an array of non-negative integers nums, you are initially positioned at the first
* index of the array.
*
* Each element in the array represents your maximum jump length at that position.
*
* Your goal is to reach the last index in the minimum number of jumps.
*
* You can assume that you can always reach the last index.
*
* Constraints:
*
* 1 <= nums.length <= 10^4
* 0 <= nums[i] <= 1000
*/
const jump = (a) => {
// Current position
let currentPosition = 0;
// Destination reached from an index
let destination = 0;
// Number of jumps required
let jumps = 0;
// Loop for the elements of the array
for (let i = 0; i < a.length - 1; i++) {
// Destination index will be maximum of the current position
// and the index that can be reached after jumping
destination = Math.max(destination, i + a[i]);
// If we need to take jump
if (currentPosition == i) {
currentPosition = destination;
jumps++;
}
}
return jumps;
};
const main = () => {
let a = [2, 3, 1, 1, 4];
console.log(jump(a));
a = [2, 3, 0, 1, 4];
console.log(jump(a));
};
main();