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

  • 相关阅读:
    配置 PHP 的 Session 存储到 Redis
    ab测试工具
    mysql:general_log 日志、数据库线程查询、数据库慢查询
    upload-labs-env文件上传漏洞 11-19关
    upload-labs-env文件上传漏洞 1-10关
    Webshell免杀绕过waf
    虚拟机安装安全狗apache服务的一些问题解决方式(11.5)
    SQL注入过WAF(11.4 第三十三天)
    内联注入和堆叠注入(10.30 第三十天)
    SQL server 注入 和 SQL server 扩展(10.29 第二十九天)
  • 原文地址:https://www.cnblogs.com/shiyublog/p/9687057.html
Copyright © 2011-2022 走看看