Skip to content

Commit

Permalink
Merge pull request #82 from BrianLusina/feat/jump-game
Browse files Browse the repository at this point in the history
Puzzles | Arrays - jump game
  • Loading branch information
BrianLusina authored Feb 14, 2024
2 parents 00e2ce8 + 52773b2 commit 66d7e30
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
* Singly Linked List
* [Node](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/linked_lists/singly_linked_list/node.py)
* [Test Singly Linked List](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/linked_lists/singly_linked_list/test_singly_linked_list.py)
* [Test Singly Linked List Pairwise Swap](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py)
* [Test Singly Linked List Rotate](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/linked_lists/singly_linked_list/test_singly_linked_list_rotate.py)
* Lists
* Bus Stops
Expand Down Expand Up @@ -358,6 +359,8 @@
* [Test Container With Most Water](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/container_with_most_water/test_container_with_most_water.py)
* Increasing Triplet Subsequence
* [Test Increasing Triplet](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/increasing_triplet_subsequence/test_increasing_triplet.py)
* Jump Game
* [Test Jump Game](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/jump_game/test_jump_game.py)
* Kid With Greatest No Of Candies
* [Test Kids With Greatest Candies](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/kid_with_greatest_no_of_candies/test_kids_with_greatest_candies.py)
* Longest Increasing Subsequence
Expand Down
29 changes: 29 additions & 0 deletions puzzles/arrays/jump_game/README.md
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
15 changes: 15 additions & 0 deletions puzzles/arrays/jump_game/__init__.py
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
21 changes: 21 additions & 0 deletions puzzles/arrays/jump_game/test_jump_game.py
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()

0 comments on commit 66d7e30

Please sign in to comment.