45. Jump Game II (JAVA)
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)的更多相关文章
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- 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 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 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- 【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 (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- [CSP-S模拟测试]:666(模拟)
题目描述 不忘初心. 小$\pi$假期在家无聊,打开了某弹幕直播网站. 突然,有一个精彩的镜头. 小$\pi$看到了满屏的$6$,其中,有$666$.也有$666666$.也有$6666666666. ...
- [CSP-S模拟测试]:Walker(数学)
题目传送门(内部题86) 输入格式 第一行$n$接下来$n$行,每行四个浮点数,分别表示变换前的坐标和变换后的坐标 输出格式 第一行浮点数$\theta$以弧度制表示第二行浮点数$scale$第三行两 ...
- python正则之特殊表达式 .*?{}
. 能匹配所有字符--单个字符,除了\n >>> re.match(r".","1") <_sre.SRE_Match object a ...
- 配置kubernetes.client的参数遇到的坑
配置kubernetes.client遇到的一些坑: 一,job-name不能重名,如果job-name已经有了,再创建job,则会发生冲突cliflict 这样将会报以下错误:Reason : Co ...
- HTTP请求方式之POST和GET的区别
GET请求方式: 如果我们的网页收集到的用户数据,他规定了,此网页用户数据用GET的请求方式去处理的话,我们会发现,比如百度,就是一个很经典的GET请求方式 当我们在百度搜索上输入一个‘java’,点 ...
- SQL Server标量函数改写内联表值函数优化案例
问题SQL: SELECT TOP 1001 ha.HuntApplicationID , ha.PartyNumber , mht.Name AS MasterHuntTypeName , htly ...
- Linux内核中的cmpxchg函数
http://www.longene.org/forum/viewtopic.php?t=2216 前几天,为了这个函数花了好多时间,由于参考的资料有误,一直都没有看明白,直到google之后,总算搞 ...
- vs2019安装
1.下载vs_enterprise.exe(建议下载到无中文无空格目录) ,这个很小,官网下载企业版即可 2.在当前目录cmd命令执行: vs_enterprise.exe --layout offl ...
- VS2010远程调试C#程序
场景: 客户的计算机运行程序出现异常,由于办公场所不在一起,无法直接在客户的机器上调试.此时希望可以直接在开发人员的计算机(本地机器)上远程调试客户机上的软件. 假设: 本地机器的系统账户为 GIS, ...
- EncodeError: 'latin-1' codec can't encode characters in position 69-70: ordinal not in range(256)
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 69-70: ordinal not in range( ...