[LC] 45. 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.
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. Solution 1:
DP time: O(N^2) -> TLE
class Solution {
public int jump(int[] nums) {
if (nums == null || nums.length <= 1) {
return 0;
}
int[] arr = new int[nums.length];
arr[0] = 0;
for (int i = 1; i < nums.length; i++) {
// initilize as -1, if set 0 leading to final res as 0
arr[i] = -1;
for (int j = 0; j < i; j++) {
if (arr[j] != -1 && j + nums[j] >= i) {
if (arr[i] == -1 || arr[j] + 1 < arr[i]) {
arr[i] = arr[j] + 1;
}
}
}
}
return arr[nums.length - 1];
}
}
Solution 2:
Greedy: O(N)
From LC, Let's say the range of the current jump is [curBegin, curEnd], curFarthest is the farthest point that all points in [curBegin, curEnd] can reach. Once the current point reaches curEnd, then trigger another jump, and set the new curEnd with curFarthest, then keep the above steps, as the following:
i == curEnd means you visited all the items on the current level. Incrementing jumps++ is like incrementing the level you are on. And curEnd = curFarthest is like getting the queue size (level size) for the next level you are traversing.
class Solution {
public int jump(int[] nums) {
if (nums == null || nums.length <= 1) {
return 0;
}
int res = 0;
int curMax = 0;
int nextMax = 0;
for(int i = 0; i < nums.length - 1; i++) {
nextMax = Math.max(nextMax, i + nums[i]);
// when last step, should not get this condition
if (i == curMax) {
res += 1;
curMax = nextMax;
}
}
return res;
}
}
[LC] 45. Jump Game II的更多相关文章
- [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 ...
- 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 ...
- Leetcode 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- String,StringBuffer与StringBuilder的区别与选择
三者的区别 String:不可变类,一旦一个对象被建立的时候,包含在这个对象中的字符串序列是不可变的,直到这个对象被销毁.StringBuffer:可变字符序列的字符串.当其对象被创建的时候,可以用a ...
- Java 面向对象异常处理,finally,覆盖时异常特点,package,import,包之间的访问(10)
Java 面向对象异常处理, finally:final 关键字的用法参考http://www.cnblogs.com/itcqx/p/5541659.html 覆盖时异常特点,package,imp ...
- 吴裕雄--天生自然 PHP开发学习:MySQL子句
<?php $con=mysqli_connect("localhost","username","password","d ...
- 用IDLE调试python程序
1. 设置断点 先放例子: import pdb a=1 b=10 pdb.set_trace()#这里是断点 c=a+b print(c) import pdb 后,用pdb.set_trace() ...
- Django专题-Cookie
Cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响, ...
- Oracle存储过程案例集合
注:使用的工具为PLSQL Developer 壹.while简单使用(替换字符串中的字符,和REPLACE效果一样) 注: 这里没有使用REPLACE函数 1.建立存储过程 CREATE OR RE ...
- 微信小程序java8 java7 java6 encryptedData 解密 异常处理
使用java8 java7 java6 解密微信小程序encryptedData可以回遇到一些错误 1.java.security.NoSuchAlgorithmException: Cannot ...
- SEO优化技巧
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...
- Drools规则引擎详解-常用的drl实例
package droolsDemo //说明:每个 drl 都必须声明一个包名,这个包名与 Java 里面的不同,它不需要与文件夹的层次结构一致, //主要用于可以根据kmodule.xml中不同的 ...
- Linux虚拟机添加硬盘
任务:添加1块硬盘,并且分1个区,挂载到/bak 第一.插上硬盘 第二.分区 第三.格式化(定义:文件系统的类型) FAT,FAT32,NTFS,ext1,ext2,ext3,ext4,.... 第 ...