zoukankan      html  css  js  c++  java
  • Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game

    Description

    Given an array of non-negative integers, you are initially positioned at the first index of the array.

    Each element in the array represents your maximum jump length at that position.

    Determine if you are able to reach the last index.

    Example 1:

    Input: [2,3,1,1,4]
    Output: true
    Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
    

    Example 2:

    Input: [3,2,1,0,4]
    Output: false
    Explanation: You will always arrive at index 3 no matter what. Its maximum
                 jump length is 0, which makes it impossible to reach the last index.

    Solution

    从nums数组末位开始向前遍历,用lastPos标记可达nums末位的最起始序号。

    即lastPos 及之后元素均可通过一定步数到达last index.

     1 class Solution:
     2     def canJump(self, nums):
     3         """
     4         :type nums: List[int]
     5         :rtype: bool
     6         """
     7         lastPos = len(nums) - 1
     8         for i in range(len(nums) - 1, -1, -1):
     9             if i + nums[i] >= lastPos:
    10                 lastPos = i
    11         return lastPos == 0

    45. Jump Game II

    Description

    Given an array of non-negative integers, you are initially positioned at the first index of the array.

    Each element in the array represents your maximum jump length at that position.

    Your goal is to reach the last index in the minimum number of jumps.

    Example:

    Input: [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.

    Note:

    You can assume that you can always reach the last index.

    Solution

    Approach 1. Dynamic Programming [Time limit exceeded]

    minstep[ind] 表示到达ind位置需要的最小步数

    minstep[ind] = min(minstep[i] + nums[i]) + 1

    即位置为i,且i + nums[i] >= ind的,可通过再走一步到达位置ind

    class Solution:
        def jump(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            minstep = [len(nums)] * len(nums)
            Max = max(nums)
            # print(minstep)
            minstep[0] = 0
            for ind in range(1, len(nums)):
                for i in range(max(0, ind - Max), ind):
                    if nums[i] >= ind - i:
                        if minstep[ind] > minstep[i] + 1:
                            minstep[ind] = minstep[i] + 1
            return minstep[len(nums) - 1]

    Time Limit Exceeded.

    91 / 92 test cases passed.

    Approach 2. 计算当前步数内可达的最远距离

    class Solution:
        def jump(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            # step: 当前走的步数
            # last:step步数内可达的最远距离(也即step步数内,last距离的格子均可到达)
            # curr: step + 1 步数内可达的最远距离(step步数内可达的格子距离+该格子最远跳到的距离)
            # 当 i > last, 则step + 1, 用curr更新last
            step = 0
            last = 0
            curr = 0
            
            for i in range(len(nums)):
                if i > last: #超过了step步数内可达的最远距离,则需要步数+1到达
                    last = curr
                    step += 1
                curr = max(curr, i + nums[i])
            return step

    Beats: 54.64%

    Runtime: 68ms

  • 相关阅读:
    Git安装及密钥的生成并上传本地文件到GitHub上
    进阶攻略|前端最全的框架总结
    进阶攻略|前端最全的框架总结
    干货|几个有用的问答平台
    干货|几个有用的问答平台
    美食篇之御桥小聚
    美食篇之御桥小聚
    Java EE (8) -- Java EE Patterns
    css---position、z-index、opacity、overflow、hover、background
    T1082 线段树练习3 codevs
  • 原文地址:https://www.cnblogs.com/shiyublog/p/9687057.html
Copyright © 2011-2022 走看看