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.

贪心算法,每次step都是选择最远的那个。

class Solution {
public int jump(int[] nums) {
if(nums.length == 1) return 0; //no need steps int start = 1; //start index of current step
int end = nums[0]; //end index of current step
int step = 1; //minimum steps
int maxStep = 0; //furthest index of current step
while(end < nums.length-1){
for(int i = start; i <= end; i++){
maxStep = Math.max(maxStep, nums[i]+i);
}
start = end+1;
end = maxStep;
step++;
}
return step;
}
}

45. Jump Game II (JAVA)的更多相关文章

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

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

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

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

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

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

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

  5. [leetcode] 45. 跳跃游戏 II(Java)(动态规划)

    45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...

  6. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

  7. 【LeetCode】45. Jump Game II

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

  8. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

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

  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. java中 Excel表实现数据导入导出

    需要引入依赖: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> < ...

  2. 第五周课程总结&试验报告

    this和super的区别 区别点 this super 属性访问 访问同类中的属性,如果本类没有此属性则从父类中继续查找 访问父类中的属性 方法 访问本类中的方法,如果本类中没有此方法,则从父类中继 ...

  3. SQL 2008建一个job

    原文地址:http://blog.csdn.net/htl258/article/details/5543694 --  Author : htl258(Tony)--  Date   : 2010- ...

  4. scss 用法 及 es6 用法讲解

    scss 用法的准备工作,下载 考拉 编译工具 且目录的名字一定不能出现中文,哪里都不能出现中文,否则就会报错 es6 用法 let 和 const  let  声明变量的方式 在 {} 代码块里面才 ...

  5. Linux下GCC去除调试信息

    如果不去除调试信息,直白点说基本上是把代码公开了,不要带-g参数,还有就是需要带 -O2 -fvisibility=hidden -s 生成程序后可以用命令strip进一步去除不需要的符号 strip ...

  6. PADS LAYOUT的一般流程

    1.概述    本文档的目的在于说明使用PADS的印制板设计软件PowerPCB进行印制板设计的流程和一些注意事项,为一个工作组的设计人员提供设计规 范,方便设计人员之间进行交流和相互检查. 2.设计 ...

  7. WPF在资源内嵌入字体

    比如需要有这种电子表的字体风格--这种样式叫 :longzhoufeng 字体 在微软的字体有 Quartz MS.TTF或者Quartz Regular.TTF字体.下面以Quartz Regula ...

  8. 阶段3 1.Mybatis_07.Mybatis的连接池及事务_6 mybatis中的事务原理和自动提交设置

    在实际的开发中,建议使用连接池的形式. JNDI的资料 H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\讲义+笔记+资料\主流框架\31.会员版(2.0)-就业课(2. ...

  9. MAVEN打包时跳过Junit测试

    我们知道,通常情况下使用maven package命令打包时,会自动执行test包下的各个单元测试. 这是因为spring-boot-maven-plugin插件已经集成了maven-surefire ...

  10. spring boot + mybatis 连接 oracle 出现 ORA-00923: 未找到要求的 FROM 关键字 错误

    1.原因 hikari 连接池配置错误,mysql和oracle的配置不一样 2.修改 spring: datasource: hikari: connection-test-query: selec ...