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 ...
随机推荐
- 创建 Smarty 对象
/* //if (!defined('INIT_NO_SMARTY')) //{ header('Cache-control: private'); header('Content-type: tex ...
- HTTP深入浅出http请求(转)-----http请求的过程和实现机制
摘要:此文章大概讲明了http请求的过程和实现机制,可以作为了解,至于请求头和响应头的具体信息需要查看下一篇随笔:Http请求详解(转)----请求+响应各字段详解 HTTP(HyperText ...
- vue根据参数不同的路由跳转以及name的作用
最近在做VUE路由跳转根据参数的值不同但是跳转的是同一个路由的功能.点击左边的目录,根据目录ID跳转不同的列表.如下图. 路由跳转的代码: this.$router.push({path: '/RFI ...
- OOM和SOF代码
OutOfMemoryError大数组,例如图片加载. public class MockOutOfMemoryError { public static void main(String[] arg ...
- 本地运行aws lambda credential 配置 (missing credential config error)
参照这篇文章 http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-sha ...
- Using FileUpload
Using FileUpload FileUpload can be used in a number of different ways, depending upon the requiremen ...
- ORACLE sqlprompt设置
SYS@orcl>defineDEFINE _DATE = "17-JUN-19" (CHAR)DEFINE _CONNECT_IDENTIFIER = ...
- 【c++进阶:c++ algorithm的常用函数】
c++ algorithm的常用函数 https://blog.csdn.net/hy971216/article/details/80056933 reverse() reverse(it,it2) ...
- 快速入门分布式消息队列之 RabbitMQ(2)
目录 目录 前文列表 RabbitMQ 的特性 Message Acknowledgment 消息应答 Prefetch Count 预取数 RPC 远程过程调用 vhost 虚拟主机 插件系统 最后 ...
- 3D聚类
1 3D聚类和普通的二维聚类实质一样,只不过维数太高了,用三维图来表示了. 下面将官网的改成只生成一个图了 #!/usr/bin/python # -*- coding:utf-8 -*- print ...