8000 jump-game-ii · malipramod/leetcode-js@c0e4578 · GitHub
[go: up one dir, main page]

Skip to content

Commit c0e4578

Browse files
committed
jump-game-ii
1 parent b45bf7b commit c0e4578

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var jump = function(nums) {
6+
let cEnd =0, cFar =0, jumps=0;
7+
for (let i = 0; i < nums.length-1; i++) {
8+
cFar = Math.max(nums[i] + i, cFar);
9+
if(i===cEnd){
10+
jumps++;
11+
cEnd = cFar;
12+
}
13+
14+
}
15+
return jumps;
16+
};
17+
18+
console.log(jump([2,3,1,1,4]));

hard/0045 jump-game-ii/readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Jump Game II
2+
3+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
4+
5+
Each element in the array represents your maximum jump length at that position.
6+
7+
Your goal is to reach the last index in the minimum number of jumps.
8+
9+
## Example
10+
11+
Input: [2,3,1,1,4]
12+
13+
Output: 2
14+
15+
Explanation: The minimum number of jumps to reach the last index is 2.
16+
Jump 1 step from index 0 to 1, then 3 steps to the last index.
17+
18+
## Note
19+
20+
You can assume that you can always reach the last index.
21+
22+
## More Info
23+
24+
<https://leetcode.com/problems/jump-game-ii/>

0 commit comments

Comments
 (0)
0