[Leetcode] 55. Jump Game

mydailylogs
|2023. 4. 27. 15:37

Solution 1

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        n = len(nums)
        dp = [False] * (n-1)
        dp += [True]

        for i in range(n-2, -1, -1):
            for j in range(i, min(n, i + nums[i] + 1)):
                if dp[j]:
                    dp[i] = True
                    break

        return dp[0]

Solution 2

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        tail = len(nums)-1
        for i in range(len(nums)-2,-1,-1):
            if(i+nums[i] >= tail):
                tail = i
        return tail == 0 

'CS > 알고리즘' 카테고리의 다른 글

[Leetcode] 15. 3Sum  (0) 2023.05.01
[Leetcode] 1. Two Sum  (0) 2023.05.01
[Leetcode] 12. Integer to Roman  (0) 2023.05.01
[Leetcode] 202. Happy Number  (0) 2023.04.27
[Leetcode] 11. Container With Most Water  (0) 2023.04.27