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.
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的更多相关文章
- LeetCode 55. 跳跃游戏(Jump Game)
题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...
- 贪心——55. 跳跃游戏 && 45.跳跃游戏II
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II
55. 跳跃游戏 问题描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
随机推荐
- Storm 中drpc调用
package storm.starter; import backtype.storm.Config; import backtype.storm.LocalCluster; import back ...
- 旧文备份:硬盘MBR引导记录损坏的一种修复方法
硬盘MBR信息损坏原因:硬盘上安装了windows XP和linux双系统,在windows下安装一套软件,破解的时候修改了硬盘的序列号,结果导致引导系统的grub无法完成linux的引导,只能进到w ...
- 删除Navicat在注册表信息
@echo offecho 正在删除navicat注册表REG DELETE HKEY_CURRENT_USER\Software\PremiumSoft\Data /fREG DELETE HKEY ...
- A+B Problem(高精)
题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式: 分两行输入a,b<=10^500 ...
- POJ 2398--Toy Storage(叉积判断,二分找点,点排序)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6534 Accepted: 3905 Descr ...
- C++创建学生类练习
/*作业,定义一个学生类*/ /*数据成员:学号.姓名.数学.英语.计算机三科成绩 *成员函数:求总成绩.求三科平均成绩.输出学生信息 *新增一个生日类 2018.4.2 */ #include &l ...
- Flask中对MySQL的基本操作
在Flask-SQLAlchemy中,插入.修改.删除操作,均由数据库会话管理. 会话用 db.session 表示.在准备把数据写入数据库前,要先将数据添加到会话中然后调用 commit() 方法提 ...
- Maven里面多环境下的属性过滤(配置)
情景:通常一个项目都为分为开发环境(dev)和测试环境(test)还有正式环境(prod),如果每次一打包都要手动地去更改配置文件,例如数据库连接配置.将会很容易出差错. 解决方案:maven pro ...
- 为什么我用了$().height()还是对不齐呢?
有一个这样的需求:有两个显示内容的框,要使他们高度一致,因为他们存放的内容多少和结构不一样,左边内容少,右边内容多.这就导致了右边会比左边高,解决方法就是超出部分用滚轮显示,那这时就先要调整右边的高度 ...
- 列表排序之NB三人组附加一个希尔排序
NB三人组之 快速排序 def partition(li, left, right): tmp = li[left] while left < right: while left < ri ...