Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 337b0fc

Browse files
committed
Add solution #45
1 parent 366a48a commit 337b0fc

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

0045-jump-game-ii.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 45. Jump Game II
3+
* https://leetcode.com/problems/jump-game-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed array of integers nums of length n.
7+
* You are initially positioned at nums[0].
8+
*
9+
* Each element nums[i] represents the maximum length of a forward
10+
* jump from index i. In other words, if you are at nums[i], you
11+
* can jump to any nums[i + j] where:
12+
* - 0 <= j <= nums[i] and
13+
* - i + j < n
14+
*
15+
* Return the minimum number of jumps to reach nums[n - 1].
16+
* The test cases are generated such that you can reach nums[n - 1].
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @return {number}
22+
*/
23+
var jump = function(nums) {
24+
let result = 0;
25+
let max = 0;
26+
let previous = 0;
27+
28+
for (let index = 0; index < nums.length - 1; index++) {
29+
max = Math.max(max, index + nums[index]);
30+
if (index === previous) {
31+
result++;
32+
previous = max;
33+
}
34+
}
35+
36+
return result;
37+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
41|[First Missing Positive](./0041-first-missing-positive.js)|Hard|
4949
42|[Trapping Rain Water](./0042-trapping-rain-water.js)|Hard|
5050
43|[Multiply Strings](./0043-multiply-strings.js)|Medium|
51+
45|[Jump Game II](./0045-jump-game-ii.js)|Medium|
5152
46|[Permutations](./0046-permutations.js)|Medium|
5253
47|[Permutations II](./0047-permutations-ii.js)|Medium|
5354
48|[Rotate Image](./0048-rotate-image.js)|Medium|

0 commit comments

Comments
 (0)