[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
class Solution:
    def maxArea(self, height: List[int]) -> int:
        left, right = 0, len(height)-1
        result = 0

        while left <= right:
            area = (right - left) * min(height[right], height[left])
            result = max(result, area)
            if height[right] < height[left]:
                right -= 1
            else:
                left += 1

        return result

'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] 55. Jump Game  (0) 2023.04.27