파비의 매일매일 공부기록

2023.02.08 Today's Challenge 본문

Problem Solving/LeetCode

2023.02.08 Today's Challenge

fabichoi 2023. 2. 8. 23:45

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

 

Jump Game II - LeetCode

Jump Game II - You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0]. Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to

leetcode.com

ChatGPT를 통해 솔루션을 받아 보았다.
ㅠ_ㅠ 이렇게 간단한 문제였다니..
혹시 영어가 문제인건가..?!

class Solution:
    def jump(self, nums: List[int]) -> int:
        n = len(nums)
        dp = [float('inf')] * n
        dp[0] = 0

        for i in range(n-1):
            for j in range(1, nums[i]+1):
                if i + j < n:
                    dp[i+j] = min(dp[i+j], dp[i] + 1)
        
        return dp[n-1]
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.02.10 Today's Challenge  (0) 2023.02.10
2023.02.09 Today's Challenge  (0) 2023.02.09
2023.02.07 Today's Challenge  (0) 2023.02.07
2023.02.06 Today's Challenge  (0) 2023.02.06
2023.02.05 Today's Challenge  (0) 2023.02.05
Comments