엔지니어 게시판
LeetCode 솔루션 분류

[2/8] 45. 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 any nums[i + j] where:

  • 0 <= j <= nums[i] and
  • i + j < n

Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

 

Example 1:

Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [2,3,0,1,4]
Output: 2

 

Constraints:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 1000
Accepted
797.2K
Submissions
2M
Acceptance Rate
39.1%

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
class Solution:
    def jump(self, nums: List[int]) -> int:

        steps = 0
        curr_end = 0
        curr_furthest = 0
        for i in range(len(nums)-1):
            curr_furthest = max(curr_furthest, nums[i] + i)

            if curr_furthest >= len(nums)-1:
                steps += 1
                break

            if i == curr_end:
                steps += 1
                curr_end = curr_furthest

        return steps                


        # dp solution w/ time O(n) - O(n^2)

        # dp = [float('inf')]*len(nums)
        # dp[0] = 0

        # for i in range(len(nums)):

        #     for j in range(i, min(i+nums[i]+1, len(nums))):
        #         dp[j] = min(dp[j],dp[i] + 1)
        
        # return dp[-1]
전체 404 / 6 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 641 명
  • 오늘 방문자 3,946 명
  • 어제 방문자 6,598 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,507 명
알림 0