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

和上个题一样,不过这个题要求出达到目的所用的最小步骤

思路:BFS,根据例子来说,首先我们获得根节点 0:2,所以我们第一步可以到达的下一层的节点为1:3,2:1,第二部可以到达第三层的节点为

2:1,3:1,4:4,即可以到达最后一个节点,在具体操作中我们只需要保存每一层的开始和结束时候的节点即可

 class Solution(object):
def jump(self, nums):
n,start,end,step = len(nums),0,0,0
while end < n-1:
step += 1
maxend = end+1
for i in range(start,end+1):
if i + nums[i] >= n-1:
return step
maxend = max(maxend,i+nums[i])
start,end = end,maxend
return step

[leetcode greedy]45. Jump Game II的更多相关文章

  1. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  2. 【LeetCode】45. Jump Game II

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

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

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

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

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

  5. LeetCode OJ 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. 20155315 2016-2017-2 《Java程序设计》第六周学习总结

    教材学习内容总结 第10章 输入与输出 1.串流设计的概念 从应用程序角度看,将数据从来源取出,可以使用输入串流,将数据写入目的地,可以使用输出串流:在Java中,输入串流代表对象为java.io.I ...

  2. 训练赛第二场C题 zoj 2339 Hyperhuffman

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2339 解题报告:题目太长了,比赛的时候根本看不懂,完了之后问了什 ...

  3. DataTable转任意类型对象List数组-----工具通用类(利用反射和泛型)

    public class ConvertHelper<T> where T : new() { /// <summary> /// 利用反射和泛型 /// </summa ...

  4. 【译】第四篇 Replication:事务复制-订阅服务器

    本篇文章是SQL Server Replication系列的第四篇,详细内容请参考原文. 订阅服务器就是复制发布项目的所有变更将传送到的服务器.每一个发布需要至少一个订阅,但是一个发布可以有多个订阅. ...

  5. shell脚本实现监控shell脚本的执行流程及变量的值

    这篇文章主要介绍了shell脚本实现监控shell脚本的执行流程及变量的值本文使用shell完成对执行过程中条件语句中的变量的变化的监控和整个程序的执行流程的观察功能,需要的朋友可以参考下 很多时候, ...

  6. 安卓ios各版本及分辨率占比

    Google Play 安装统计数据 只有安卓的 https://developer.android.com/about/dashboards/index.html?hl=zh-cn 腾讯移动分析 安 ...

  7. HDU 3374 String Problem(KMP+最大(最小)表示)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...

  8. C++ 必须使用初始化列表

    继承关系中,父类无默认构造函数 类类型类成员变量无默认构造函数 const类型成员变量 引用类型成员变量 不使用初始化列表,在创建对象调用构造函数之前会对所有的成员变量进行默认初始化,然后再执行构造函 ...

  9. The Art Of Computer Programming: 1.1

    The Art Of Computer Programming: 1.1 */--> div.org-src-container { font-size: 85%; font-family: m ...

  10. appium----【Mac】address already in user 127.0.0.1:4725,端口被占用的查找与kill进程

    报错截图示例: 解决方法: Mac: lsof -i tcp:4723   #查看端口号   sudo kill -9 29443   #杀死进程   Windows: netstat -aon|fi ...