(WIP) leetcode 55. Jump Game (DP, medium)

wonderful world·2022년 1월 29일
0

leetcode

목록 보기
20/21

https://leetcode.com/problems/jump-game/

time limit exceeded
even if using DP(memoization)..
why?

# 55. Jump Game (DP, medium)
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        def jump(i, memo):
            if i >= len(nums): return False
            # reached at last index
            if i == len(nums)-1: return True
            if i in memo: return memo[i]

            # enumerate all the possible choices
            length = nums[i]
            reachable = False
            for j in range(1, length+1):
                if jump(i+j, memo):
                    reachable = True
                    break
            
            memo[i] = reachable
            return reachable

        return jump(0, {})
profile
hello wirld

0개의 댓글