[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 ...
随机推荐
- rabbit-mq cluster安装
Centos6.5 安装 RabbitMQ3.6.5 一.安装编译工具 yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel op ...
- Java Properties基础知识总结
在Java语言中,使用一种以.properties为扩展名的文本文件作为资源文件,该类型的文件的内容格式为类似: some_key=some_value #注释描述 还有一种是使用xml文件保存项目的 ...
- js中call和apply的实现原理
js中call和apply的实现原理 实现call的思路: /* 还有就是call方法是放在Function().prototype上的也就是构造函数才有的call方法 (我门可 ...
- pandas(二)
1.Series序列 一维的数组数据,构建是传二维数据会报错,数据具有索引,构建时如果不传索引,默认为数字rang索引. series存在列名和索引,sr.at[0]是通过列名来定位数据(iat定位行 ...
- c#学习笔记05——数组&集合
数组 声明数组 .一维数组的定义: 数据类型[] 数组名=new 数据类型[大小]; eg: ]; ,,,,}; ]; .多维数组的定义 ,];//定义二维数组 ,,];//定义三维数组 多维数组可以 ...
- Python—构造单向链表数据类型
# _*_ coding=utf-8 _*_ class Node: """ 创建链表的属性 """ def __init__(self, ...
- Linux-proc文件系统介绍
1.操作系统级别的调试 (1).简单程序单步调试 (2).复杂程序printf打印信息调试 (3).框架体系日志记录信息调试 (4).内核调试的困境 2.proc虚拟文件系统的工作原理 (1).Lin ...
- redis安装以及主从复制完整版
redis安装以及主从复制完整版redis版本:redis-3.2.11主从复制模式:master--> slave1--> slave2 master:10.10.11.32 slave ...
- 0.3W微功率放大器
电路结构 电路摘自<晶体管电路设计(上)>. 电路采用+5V单电源供电,两级结构.Tr1构成共射极放大电路作为电压放大级:Tr3,Tr4构成推挽的射极跟随器作为输出级:Tr2作为射极跟随器 ...
- 《后端也要懂一点前端系列》使用webpack搭建项目
今天突然有兴致想要学习一下前端的技术,所以特此记录学习前端之路.由于之前在公司做的项目大部分都是关于JSP页面的增删改查,所以前端后端都是一个人来写的,对于前端还只是停留在js.html.css阶段, ...