【LeetCode每天一题】Jump Game II(跳跃游戏II)
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.
思路
这个我们可以参考跳跃游戏I的那个接替思路,只不过这里我们需要增加一个下标变量来记录step数。其余的都和跳跃游戏I一样。时间复杂度为O(n),空间复杂度为O(1)。
图示步骤

解决代码
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_end = 0
end = 0
step = 0 # 记录步数
for i in range(len(nums)-1):
max_end = max(max_end, nums[i]+i)
if i == end: # 跟新最大下标位置,并步数加一
end = max_end
step += 1
return step
【LeetCode每天一题】Jump Game II(跳跃游戏II)的更多相关文章
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...
- Leetcode力扣45题 跳跃游戏 II
原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...
- LeetCode 45. 跳跃游戏 II | Python
45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- Java实现 LeetCode 45 跳跃游戏 II(二)
45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- [LeetCode每日一题]81. 搜索旋转排序数组 II
[LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...
- leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II
55. 跳跃游戏 问题描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
随机推荐
- vue的js文件中获取vue实例
1.main.js导出vue实例: var vue = new Vue({ el: '#app', router, components: { App }, template: '<App/&g ...
- virtualbox安装android6.0并设置分辨率为1920x1080x32
下载安装:https://www.cnblogs.com/wynn0123/p/6288344.html 这里我做的是下载android6.0-64bit,然后文件系统只支持ext4 安装完成之后我的 ...
- linux下pppoe连接管理
一.安装pppoe组件 sudo apt-get install pppoe pppoeconf 二.配置pppoe 图形界面配置pppoe,在terminal里输入 nm-connection-ed ...
- iOS10.3 起,将支持应用内评分
iOS10.3 起,将支持应用内评分. (2017-01-25,现在最高版本iOS10.2.1,Xcode 稳定版本Xcode8.2.1,Xcode Beta版本 Xcode8.3Beta(BW109 ...
- Excel公式笔记
跨表,查找相同的数据,的其他列的值 =index(sheet2!$c$:$c$,match(sheet1!$b$,sheet2!$d$:$d$,)) 备注: =index(读哪里的数据(大范围), m ...
- UITableView 自定义多选
前言 在上一篇文章中介绍了UITableView的多选操作,有提到将 return UITableViewCellEditingStyleDelete | UITableViewCellEditing ...
- python机器学习包 Windows下 pip安装 scikit-learn numpy scipy
1.到PIP的目录中C:\Python34\Scripts;2. 2.1 pip安装numpy pip install numpy 2.2 pip安装sklearn pip install -U ...
- 给vscode添加右键打开功能
将以下文本存为vscode.reg,然后运行: Windows Registry Editor Version 5.00 ; Open files [HKEY_CLASSES_ROOT\*\shel ...
- 恒生UFX交易接口基本介绍说明
1.恒生UFT和UFX有什么区别? UFT是一个极速交易系统,UFX是一个统一接入系统.交易系统很显然是可以进行股票交易的,UFX是所有后台交易系统的接入系统,不管后台是什么样子的交易系统都是可以通过 ...
- webpack使用小记
前言 webpack是目前前端开发必不可少的一款模块加载器兼构建工具,它能极其方便的处理各种资源的打包和使用, 让前端开发获得与后端开发几乎一致的体验. webpack特点 webpack 是以 co ...