본문 바로가기

LEETCODE

#55. Jump Game

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

def canJump(self, nums: List[int]) -> bool:
        maxReach = 0
        for i in range(len(nums)):
            if i > maxReach: # Cannot reach last index
                return False
            maxReach = max(maxReach, i + nums[i])

            if maxReach >= len(nums): # Can reach (moved beyond)
                return True
        return True # Can reach (exactly)

'LEETCODE' 카테고리의 다른 글

#54. Spiral Matrix  (0) 2023.11.01
#62. Unique Paths  (1) 2023.11.01
#53. Maximum Subarray  (0) 2023.11.01
#34. Find First and Last Position of Element in Sorted Array  (0) 2023.11.01
#13. Roman to Integer  (0) 2023.10.19