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, ...
随机推荐
- 使用TextView/EditText应该注意的地方,监听EditText,addTextChangedListener
http://blog.csdn.net/huichengongzi/article/details/7818676 监听 EditText 控件: addTextChangedListener(ne ...
- 【题解】P1516 青蛙的约会(Exgcd)
洛谷P1516:https://www.luogu.org/problemnew/show/P1516 思路: 设两只青蛙跳了T步 则A的坐标为X+mT B的坐标为Y+nT 要使他们相遇 则满足: ...
- AngularJS 四 服务
AngularJS服务: API: https://docs.angularjs.org/api/ng/service 下面只会介绍几种,需要的话可以去官网查看 AngularJS服务 ...
- 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f4-hystrix/ 本文出自方志朋的博客 在微服务架构中, ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--F-等式
链接:https://www.nowcoder.com/acm/contest/90/F 来源:牛客网 1.题目描述 给定n,求1/x + 1/y = 1/n (x<=y)的解数.(x.y.n均 ...
- flask之请求钩子
from flask import Flask from flask import abort app = Flask(__name__) # 在第一次请求之前调用,可以在此方法内部做一些初始化操作 ...
- XAMPP中的MySQL与本地MySQL冲突的问题
学习SQL时在本地中先安装了MySQL,后来因为项目需要又安装了XAMPP集成环境,今天在启动项目的时候发现启动MySQL各种问题(期望启动的是XAMPP中的MySQL服务),在Navicat中显示成 ...
- LeetCode 相交链表
基本思路 先计算出两个链表的长度 O(n) 将长的一个链表的指示指针移动到和短链表相同长度 O(n) 两个链表指示指针同时向前移动,直到二者相同或者NULL 代码实现 /** * Definition ...
- tomcat6添加服务
Mysql在导入大量数据的时候就要把tomcat添加成服务 添加服务 在DOS界面下,进入Tomcat解压目录的bin目录 service.bat install
- Linux入门-第六周
1.总结IP地址规划 IP地址的合理规划是网络设计中最重要的一环,在大型网络中必须对IP地址进行统一规划并得到实施.IP地址规划的好坏影响到网络路由协议算法的效率,影响到网络的性能,影响到网络的拓展, ...