-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from BrianLusina/feat/jump-game
Puzzles | Arrays - jump game
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Jump Game | ||
|
||
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the | ||
array represents your maximum jump length at that position. | ||
|
||
Return true if you can reach the last index, or false otherwise. | ||
|
||
```text | ||
Example 1: | ||
Input: nums = [2,3,1,1,4] | ||
Output: true | ||
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. | ||
``` | ||
|
||
```text | ||
Example 2: | ||
Input: nums = [3,2,1,0,4] | ||
Output: false | ||
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible | ||
to reach the last index. | ||
``` | ||
|
||
## Topics | ||
|
||
- Array | ||
- Dynamic Programming | ||
- Greedy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import List | ||
|
||
|
||
def can_jump(nums: List[int]) -> bool: | ||
current_position = nums[0] | ||
|
||
for idx in range(1, len(nums)): | ||
if current_position == 0: | ||
return False | ||
|
||
current_position -= 1 | ||
|
||
current_position = max(current_position, nums[idx]) | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import unittest | ||
|
||
from . import can_jump | ||
|
||
|
||
class CanJumpTestCase(unittest.TestCase): | ||
def test_1(self): | ||
"""nums = [2,3,1,1,4] should return true""" | ||
nums = [2, 3, 1, 1, 4] | ||
actual = can_jump(nums) | ||
self.assertTrue(actual) | ||
|
||
def test_2(self): | ||
"""nums = [3,2,1,0,4] should return false""" | ||
nums = [3, 2, 1, 0, 4] | ||
actual = can_jump(nums) | ||
self.assertFalse(actual) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |