Skip to content

Commit

Permalink
feat(leetcode): add sol for prob 45
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishdotme committed May 13, 2023
1 parent d57d5d3 commit 8614524
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/leetcode.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [35. Search insert position](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/035-search-insert-position.js)
- [38. Count and say](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/038-count-and-say.js)
- [41. First missing positive](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/041-first-missing-positive.js)
- [45. Jump game 2](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/045-jump-game-2.js)
- [48. Rotate image](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/048-rotate-image.js)
- [49. Group anagrams](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/049-group-anagrams.js)
- [53. Maximum subarray](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/053-maximum-subarray.js)
Expand Down
25 changes: 25 additions & 0 deletions leetcode/045-jump-game-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
*
* Ashish Patel
* e: ashishsushilPatel@gmail.com
* w: https://ashish.me
*
*/

const jumpGame2 = nums => {
let left = 0
let right = 0
let result = 0
while (right < nums.length - 1) {
let max = 0
for (let i = left; i < right + 1; i++) {
max = Math.max(max, i + nums[i])
}
left = right + 1
right = max
result += 1
}
return result
}

console.log(jumpGame2([2, 3, 1, 1, 4]))
1 change: 1 addition & 0 deletions leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [35. Search insert position](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/035-search-insert-position.js)
- [38. Count and say](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/038-count-and-say.js)
- [41. First missing positive](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/041-first-missing-positive.js)
- [45. Jump game 2](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/045-jump-game-2.js)
- [48. Rotate image](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/048-rotate-image.js)
- [49. Group anagrams](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/049-group-anagrams.js)
- [53. Maximum subarray](https://github.com/ashishdotme/code.ashish.me/blob/master/leetcode/053-maximum-subarray.js)
Expand Down

0 comments on commit 8614524

Please sign in to comment.