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, ...
随机推荐
- 【luogu P1962 斐波那契数列】 题解
题目链接:https://www.luogu.org/problemnew/show/P1962 给你篇dalao的blog自己看吧,把矩阵快速幂的板子一改就OK #include <algor ...
- android 网络技术基础学习 (七)
使用httpclient协议访问网络: public class MainActivity extends Activity implements OnClickListener{ public vo ...
- LeetCode12.整数转罗马数字 JavaScript
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- Restframework中的Request
1.介绍 该篇博客主要介绍restframework内置的Request类,它扩展了Django中的Request类,实现了很多方便的功能--如请求数据解析和认证等. 如: 在APIView中封装的r ...
- JSP的小心得
问题:Web容器(例如Tomcat)是怎么来执行jsp文件的? 首先它会将放在webapps目录下的jsp文件(这里以hello.jsp为例)翻译成hello_jsp.java文件并编译为hello_ ...
- java HtmlEmail发送邮件工具类
package com.sh.xrsite.common.utils; import java.io.File; import java.util.HashMap; import java.util. ...
- 【杂题总汇】AGC027 C - ABland Yard
◆AGC027◆C - ABland Yard 终于知道为什么比赛的时候这么多人做C题了…… +传送门+(这是beta版的) ◇ 题目(自己翻译的,不要在意细节……) P.S. (@ 2018-9-2 ...
- 上传文件到阿里云linux服务器
在“运行”中输入cmd,打开控制台,切换到刚才Putty的安装目录下,我的是E:\Putty,然后输入pscp命令,我们需要这个命令来实现文件的上传.如下图所示,命令格式为: pscp D:\recy ...
- 使用NPOI快速导出导入Excel
这两天做项目需要导入导出EXCEL,是基于NPOI的封装,设计思路是使用DataTable,然后导出一个和DataTable一模一样的Excel表出来 github地址:https://github. ...
- laydate js动态添加时间
$("#test2").click(function(){ var input=$('<input/>'); $("#test1").append( ...