File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 484841|[ First Missing Positive] ( ./0041-first-missing-positive.js ) |Hard|
494942|[ Trapping Rain Water] ( ./0042-trapping-rain-water.js ) |Hard|
505043|[ Multiply Strings] ( ./0043-multiply-strings.js ) |Medium|
51+ 45|[ Jump Game II] ( ./0045-jump-game-ii.js ) |Medium|
515246|[ Permutations] ( ./0046-permutations.js ) |Medium|
525347|[ Permutations II] ( ./0047-permutations-ii.js ) |Medium|
535448|[ Rotate Image] ( ./0048-rotate-image.js ) |Medium|
You can’t perform that action at this time.
0 commit comments