[LeetCode][Java] 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.)
题意:
给定一个非负整数数组。给定的初始化位置在数组的起始位置。
数组中的每一个元素代表着你能都在此位置跳跃的最大的距离。
你的目标是用最少的跳跃数达到数组的末尾。
比方:给定A = [2,3,1,1,4]
达到数组尾部的最小的跳跃步数为2。(用1步从索引 0 到 1, 接着用3步到达结尾索引。)
算法分析:
该题思想主要是。扫描数组,以确定当前最远能覆盖的节点。放入maxreach。
然后继续扫描,直到当前的路程超过了上一次算出的覆盖范围reach,那么更新覆盖范围,同一时候更新条数,由于我们是经过了多一跳才干继续前进的。
形象地说,这个是在争取每跳最远的greedy.
* ret:眼下为止的jump数
* curRch:从A[0]进行ret次jump之后达到的最大范围
* curMax:从0~i这i+1个A元素中能达到的最大范围
* 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素。因此须要添加一次jump,使之达到
* 记录的curMax。
AC代码:
/**
* ret:眼下为止的jump数 * curRch:从A[0]进行ret次jump之后达到的最大范围 * curMax:从0~i这i+1个A元素中能达到的最大范围 * 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素,因此须要添加一次jump。使之达到 * 记录的curMax。 */
public class Solution
{
public int jump(int[] nums)
{
int ret = 0;
int curMax = 0;
int curRch = 0;
for(int i = 0; i < nums.length; i ++)
{
if(curRch < i)
{
ret ++;
curRch = curMax;
}
curMax = Math.max(curMax, nums[i]+i);
}
return ret;
}
}
[LeetCode][Java] Jump Game II的更多相关文章
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode]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 (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode 045 Jump Game II
题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...
- Java for LeetCode 045 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 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- JavaSE-14 异常处理
学习要点 使用try-catch-finally处理异常 使用throw.throws抛出异常 异常及其分类 log4j记录日志 异常 1 异常的定义 异常是指在程序的运行过程中所发生的不正常的事件 ...
- No-2.注释
01. 注释的作用 使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释) 以 # 开头,# 右边的所有东西都被当做说明文字,而不是真正要执行的程序,只起到 ...
- impdp导入
//导入命令impdp 用户/密码@数据库链接 directory=数据库中映射路径 schemas=原用户 remap_schema=原用户:现用户 remap_tablespace=原表空间:现表 ...
- aggregate和annotate方法使用详解与示例
aggregate和annotate方法的使用场景 Django的aggregate和annotate方法属于高级查询方法,主要用于组合查询.当我们需要对查询集(queryset)的某些字段进行计算或 ...
- MySQL Connector/Python 接口 (三)
本文参见这里. 使用缓冲的 cursor,下例给从2000年加入公司并且还在公司的员工薪水从明天起加15% from __future__ import print_function from dec ...
- 字符串String对象构造方法的创建和直接赋值的区别
/* * 通过构造方法创建的字符串对象和直接赋值方式创建的字符串对象有什么区别呢? * 区别是:通过构造方法创建的字符串对象是在堆内存.通过赋值创建的字符串对象是在方法区的常量池 * * == * 基 ...
- python virtualenv 管理工具 - virtualenvwrapper
我们使用python virtualenv构建不同的python环境,python3 也加入了virtualenv 模块. virtualenvwrapper 提供了更便捷的 virtualenv环境 ...
- Leetcode 204计数质数
计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 比计算少n中素数的个数. 素数又称质 ...
- DP 简单题目练习
ZOJ 1234 这道题目我表示也还不是特别能理解....还是太菜了T T 从后往前思考,因为只要后面有多的数在,那么C肯定是存在的,只要考虑是否把前两个数加在一起作为badness值这样两种情况来考 ...
- bzoj 1430 小猴打架 prufer 性质
小猴打架 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 709 Solved: 512[Submit][Status][Discuss] Descri ...