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. 【luogu P1004 方格取数】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1004 标准的DP,不明白为什么有普及+提高的难度 四维DP[i][j][k][l] 表示第一遍走到i,j格子 ...

  2. 旧文备份:硬盘MBR引导记录损坏的一种修复方法

    硬盘MBR信息损坏原因:硬盘上安装了windows XP和linux双系统,在windows下安装一套软件,破解的时候修改了硬盘的序列号,结果导致引导系统的grub无法完成linux的引导,只能进到w ...

  3. selenium之Xpath定位

    1. 绝对定位: driver.find_element_by_xpath("/html/body/div[x]/form/input") x 代表第x个 div标签,注意,索引从 ...

  4. 自动化维护任务 – Automated Maintenance Task (转)

    1. Oracle有三个已定义好的automated maintenance tasks. Automatic Optimizer Statistics Collection—用于收集各种数据库对象的 ...

  5. 概括iOS知识点思维导图

  6. JAVA如何跨项目调用接口

    public String load(String url, String query) throws Exception { URL restURL = new URL(url); /* * 此处的 ...

  7. [HNOI2003]操作系统(优先队列,堆排序)

    题目描述 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示,数字越大,则优先级越高. 如果一个进程到达的时 ...

  8. mysql 排名

    一.sql1{不管数据相同与否,排名依次排序(1,2,3,4,5,6,7.....)} SELECT obj. AS rownum FROM ( SELECT user_id, score FROM ...

  9. Percona XtraDB Cluster 5.7安装配置

    优点:1.准同步复制2.多个可同时读写节点,可实现写扩展,较分片方案更进一步3.自动节点管理4.数据严格一致5.服务高可用缺点:1.只支持innodb引擎2.所有表都要有主键3.所有的写操作都将发生在 ...

  10. pyqt 多窗口跳转

    今天在做pyqt5的多页面跳转时遇到问题,一点击button按钮,程序会崩溃.在网上查了下,应该是当窗口A调用窗口B的时候,两个窗口不能是同一类型.我写的时候把A.B同时写成了QWidget.把窗口B ...