# -*- 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的更多相关文章

  1. 【LeetCode】45. Jump Game II

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  2. 【LeetCode】45. Jump Game II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  3. 【一天一道LeetCode】#45. Jump Game II

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

  4. LeetCode OJ 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. [leetcode greedy]45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  7. 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 ...

  8. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  9. [LeetCode] 45. Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. Asp.net 处理程序(第五篇)

    HttpApplication有19个标准事件,当到达第8个事件PostMapRequestHandler触发的时候,标志着已经获取到了处理请求的处理程序对象,在第11个事件PreRequestHan ...

  2. C语言随笔_类型声明

    有位同学说,“老师,我运行如下代码,结果报错了” #include <iostream.h>   int main(){ char c,  int b; return 0; } 报错结果是 ...

  3. FZU 1856 The Troop (JAVA高精度)

    Problem 1856 The Troop Accept: 72    Submit: 245Time Limit: 1000 mSec    Memory Limit : 32768 KB Pro ...

  4. 在国内使用cnpm代替npm

    npm是Node.js的模块依赖管理工具,由于使用npm安装包是从国外服务器下载,在国内很容易受到网络的影响,速度非常慢,因此可以选用cnpm.cnpm可以使用淘宝团队提供的淘宝npm镜像,你可以用此 ...

  5. MyEclipse通过JDBC连接MySQL数据库基本介绍

    转载自:http://www.jb51.net/article/31876.htm 1.前提是MyEclipse已经能正常开发Java工程 2.安装MySQL 个人使用的是版本是 mysql-5.0. ...

  6. vector之妙用系列

    vector用法: 总结了下大家写的,感觉用着很方便: vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是 ...

  7. 高效CSS書寫規範及CSS兼容性

    一.選擇器針對性說明 某一元素的多个规则集中,选择器的针对性越高,该规则集的权重也就越高.针对性相同的,后出现的规则集的权重更高. * {} /* a=0 b=0 c=0 d=0 -> spec ...

  8. 使用java连接hive,并执行hive语句详解

    安装hadoop 和 hive我就不多说了,网上太多文章 自己看去 首先,在机器上打开hiveservice hive --service hiveserver -p 50000 & 打开50 ...

  9. PHP流程控制中不经常使用的替代语法

    准备做个wordpress的主题.结果看到了例如以下的语法: <div id="primary" class="content-area"> < ...

  10. 【高精度+DP】【HDU1223】 OrderCount

    Order Count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...