diff --git a/src/jump_game_ii.py b/src/jump_game_ii.py new file mode 100644 index 0000000..b43411e --- /dev/null +++ b/src/jump_game_ii.py @@ -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 diff --git a/tests/test_jump_game_ii.py b/tests/test_jump_game_ii.py new file mode 100644 index 0000000..52b8f4c --- /dev/null +++ b/tests/test_jump_game_ii.py @@ -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