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.

 class Solution:
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
lastPos = len(nums) - 1
for i in range(len(nums) - 1, -1, -1):
if i + nums[i] >= lastPos:
lastPos = i
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

Leetcode 55. Jump Game & 45. Jump Game II的更多相关文章

  1. LeetCode 55. 跳跃游戏(Jump Game)

    题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...

  2. 贪心——55. 跳跃游戏 && 45.跳跃游戏II

    给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...

  3. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  4. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  5. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  6. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

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

  7. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

  8. [LeetCode] 55. Jump Game 跳跃游戏

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

  9. leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II

    55. 跳跃游戏 问题描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...

随机推荐

  1. Storm 出现 no jzmq in java.library.path

    在真实环境中运行时,在log日志下,查看workpid日志发现出现该错误. 解决办法: 在conf/storm.yaml添加jzmq安装的路径, 我使用的默认安装在/usr/local/lib下 ja ...

  2. iOS程序的启动原理

    一.在一些老的项目中,有很多不是通过代码添加窗口和跟控制器.是通过设置MainInterface(设置最主要的xib),在xib中通过图形化的操作设置应用程序的代理->设置窗口->设置根控 ...

  3. Java8函数之旅 (六) -- 使用lambda实现Java的尾递归

    前言 本篇介绍的不是什么新知识,而是对前面讲解的一些知识的综合运用.众所周知,递归是解决复杂问题的一个很有效的方式,也是函数式语言的核心,在一些函数式语言中,是没有迭代与while这种概念的,因为此类 ...

  4. 如何对Project Proffesional设置预警灯

    Project Proffesional没法一目了然地看到,为了实时看到任务延迟情况,我们必须设置预警灯. 1.添加两个新列“文本1”.“文本2”,重命名为“完成预警”.“进度预警”. 2.右键点击“ ...

  5. 配置web项目session永不超时

    众所周知,当用户登录网站后较长一段时间没有与服务器进行交互,将会导致服务器上的用户会话数据(即session)被销毁.此时,当用户再次操作网页时,如果服务器进行了session校验,那么浏览器将会提醒 ...

  6. spring boot整合mybatis查询数据库返回Map字段为空不返回解决

    1.出现问题原因原因1:mybatis的配置即mapper返回映射配置. 原因2:jackson的配置即@ResponseBody序列化配置. 2.解决方式步骤1:解决原因1 mybatis: con ...

  7. Hibernate知识点小结(三)-->一对多与多对多配置

    一.多表关系与多表设计 1.多表关系        一对一:            表的设计原则(分表原则):                优化表的性能                基于语意化分表 ...

  8. ABAP术语-Field

    Field 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/01/1061244.html Name in an ABAP program f ...

  9. 小程序swiper不显示图片

    按照文档上的代码运行后,发现图片不显示 解决办法: app.wxss文件 align-items: center;这句话删除了,运行 OK!

  10. linux tail + head 查看指定行

    取出一段数据后,需要获取指定行 file # 前10行 file # 不要最后10行的前面所有行 file # 后10行 file # 不要前面10行的后面所有行 | # 不要前后10行剩余的所有行