From d39cd87c89a8992b1f8829a3135f7b0190486729 Mon Sep 17 00:00:00 2001 From: BrianLusina <12752833+BrianLusina@users.noreply.github.com> Date: Wed, 14 Feb 2024 08:57:49 +0300 Subject: [PATCH 1/2] feat(puzzles): arrays - jump game --- puzzles/arrays/jump_game/README.md | 29 ++++++++++++++++++++++ puzzles/arrays/jump_game/__init__.py | 15 +++++++++++ puzzles/arrays/jump_game/test_jump_game.py | 21 ++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 puzzles/arrays/jump_game/README.md create mode 100644 puzzles/arrays/jump_game/__init__.py create mode 100644 puzzles/arrays/jump_game/test_jump_game.py diff --git a/puzzles/arrays/jump_game/README.md b/puzzles/arrays/jump_game/README.md new file mode 100644 index 00000000..66b7afb3 --- /dev/null +++ b/puzzles/arrays/jump_game/README.md @@ -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 diff --git a/puzzles/arrays/jump_game/__init__.py b/puzzles/arrays/jump_game/__init__.py new file mode 100644 index 00000000..a06998e5 --- /dev/null +++ b/puzzles/arrays/jump_game/__init__.py @@ -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 diff --git a/puzzles/arrays/jump_game/test_jump_game.py b/puzzles/arrays/jump_game/test_jump_game.py new file mode 100644 index 00000000..5a949ccb --- /dev/null +++ b/puzzles/arrays/jump_game/test_jump_game.py @@ -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() From 52773b26eec3e7aa9c5bfaa2c80bc0a2b312361d Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 14 Feb 2024 05:58:11 +0000 Subject: [PATCH 2/2] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 981a4971..18507d19 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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 @@ -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