Skip to content

Commit

Permalink
"Jump Game II" solution
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Jul 30, 2024
1 parent 151cfa1 commit ad3c134
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/jump_game_ii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def jump(self, nums: list[int]) -> int:
if len(nums) == 1:
return 0

i = 1
count = 1

jump = nums[0]

while jump < len(nums) - 1:
next_jump = 0

while i <= jump and i < len(nums):
next_jump = max(next_jump, nums[i] + i)
i += 1

jump = next_jump
count += 1

return count
17 changes: 17 additions & 0 deletions tests/test_jump_game_ii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from src.jump_game_ii import Solution


@pytest.mark.parametrize(
"nums,expected",
(
([2, 3, 1, 1, 4], 2),
([2, 3, 0, 1, 4], 2),
([0], 0),
([2, 1], 1),
([1, 2, 3], 2),
),
)
def test_solution(nums, expected):
assert Solution().jump(nums) == expected

0 comments on commit ad3c134

Please sign in to comment.