[Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 45: Jump Game II
https://oj.leetcode.com/problems/jump-game-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. For example:
Given array A = [2,3,1,1,4] 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.) ===Comments by Dabay===
上一次的最远边界,与这一步能够达到的最远边界组成一个范围段。这是第N步能够到的最远范围。
如果这个范围超过了数组的边界,返回步数。
''' class Solution:
# @param A, a list of integers
# @return an integer
def jump(self, A):
if len(A) <= 1:
return 0
last_index = len(A) - 1
min_index = 1
max_index = A[0]
step = 1
if max_index >= last_index:
return step
while True:
new_max_index = min_index
step = step + 1
for index in xrange(min_index, max_index+1):
reach = index + A[index]
new_max_index = max(new_max_index, reach)
if new_max_index >= last_index:
return step
else:
min_index, max_index = max_index+1, new_max_index def main():
s = Solution()
print s.jump([2,3,0,1,4]) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]45: Jump Game II的更多相关文章
- 【LeetCode】45. Jump Game II
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【LeetCode】45. Jump Game II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- 【一天一道LeetCode】#45. Jump Game II
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- LeetCode OJ 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode greedy]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(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- 文档生成工具doxygen+图像生成工具GraphViz
文档生成工具doxygen+图像生成工具GraphViz 虽然jdk自带的javadoc也很好用,不过使用doxygen+GraphViz 的组合可以生成许多强大的图(类图.协作图.文件包含/被包含图 ...
- UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>
B - 秋实大哥带我飞 Time Limit: 300/100MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- 求解答,Android源码编译时怎样添加第三方jar包
各位大神好,遇到的问题如标题. 我用Eclipse写了一个android工程,但是这个工程需要到SDK的隐藏类,所有想在源码下编译,但是每次mm之后,都会出现错误,提示是找不到对应的类. 我需要加入的 ...
- jar 查找多jar包中类的办法
jar -tf 多个文件列表, 如jar -tf *.jar 或 jar -tf a.jar b.jar ,这样是无任何输出的. 解决办法为: find . -name "*.j ...
- java一个简单的管理系统
用java实现的简单管理系统 运行出来的状态 实现了新增.删除.借出.归还.排行榜简单的功能! 下面是简单的代码 首先定义一个书籍类,自己打开哦! public class Book implemen ...
- Can you solve this equation?(二分)
Can you solve this equation? Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Ja ...
- linux光盘、U盘的挂载与卸载
mount [-t vfstype] [-o options] device dir1.-t vfstype 指定文件系统的类型,通常不必指定.mount 会自动选择正确的类型.关于一些常用的文件:i ...
- 解决net-snmp正确输出MAC地址和判断空的IP地址
function readVarbinds (buffer, varbinds) { buffer.readSequence (); while (1) { buffer.readSequence ( ...
- ORACLE 横表与纵表
一.横表和纵表 横表:通常指我们平时在数据库中建立的表,是一种普通的建表方式. (主键.字段1.字段2......)如:时间.客户ID,基本通话费.漫游通话费,国内长途费.国际长途费... ...
- Eclipse编译Arduino程序不能使用串口函数Serial.begin解决办法
在Arduino官方的编译器当中Serial.begin(9600);初始化语句是可以直接使用的,而到Eclipse当中,同样的语句却不能用了.会出现下面的问题: 显然,这是Eclipse没有找到Se ...