algos_and_structures/leetcode/array_55_jump_game/my_solution.py
2024-11-02 14:03:30 +03:00

32 lines
989 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Не решил ...
from typing import List
class Solution:
def canJump(self, nums: List[int]) -> bool:
def calculate_jump_for_elem(elem, elem_index):
if elem == len(nums):
return True
for jump_distance in range(elem, -1, -1):
if (elem_index + jump_distance) == len(nums) - 1:
return True
if jump_distance == 0:
continue
jump_index = elem_index + jump_distance
if jump_index > len(nums) - 1:
continue
next_elem = nums[jump_index]
return calculate_jump_for_elem(next_elem, jump_index)
res = calculate_jump_for_elem(nums[0], 0)
return res if res else False
sl1 = Solution()
print(sl1.canJump(nums=[2, 3, 1, 1, 4]))
print(sl1.canJump(nums=[3, 2, 1, 0, 4]))
print(sl1.canJump(nums=[1]))
print(sl1.canJump(nums=[1, 2, 3]))
print(sl1.canJump(nums=[2, 5, 0, 0]))