🔍 알고리즘/Leetcode

[Python] Leetcode 55. Jump Game (Medium)

탄치 2022. 7. 27. 20:51

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

 

Jump Game - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

배열의 마지막에 도달할 수 있는지 묻는 문제입니다.

저는 거꾸로 배열의 마지막에서 시작해 어떤 위치까지 갈 수 있는지의 여부를 갱신하고 만약 시작점에서도 가능하다면 True를 리턴하는 식으로 풀이했습니다.

 

class Solution:
    def canJump(self, nums: list) -> bool:
        idx = len(nums)-2
        reach = len(nums)-1
        
        while(idx >= 0):
            if(nums[idx] + idx >= reach):
                reach = idx
            idx -= 1
            
        return reach == 0
    

input = [[2,3,1,1,4],[3,2,1,0,4],[2,0,0]]

for i in input:
    print(Solution().canJump(i))

 

728x90