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的更多相关文章

  1. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  2. 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 ...

  3. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  4. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  5. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

  6. 【LeetCode】45. Jump Game II

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  7. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  8. [LeetCode] 45. Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. Leetcode 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. Linux-socket编程接口介绍

    1.建立连接 (1).socket.socket函数类似于open,用来打开一个网络连接,如果打开成功则返回一个网络文件描述符(int类型),之后我们操作这个网络连接都可以通过这个网络文件描述符. ( ...

  2. 通过ES6 封装了一个上传文件的方法 XMLHttpRequest() 通用

    ### 上传进度回显,上传速度回显 ### 源码如下,新建index.js装起来 export class UploadServers { constructor (options) { this.x ...

  3. git 知识罗列

    git pull is basically a shortcut for two operations: git fetch which downloads the history from the ...

  4. h5-渐变的基本描述

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. POJ 1201 Intervals【差分约束】

    传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...

  6. 股票数据的原始数据形态&数据驱动来设计金融股票业务场景

    1. 数据源 其实金融数据没大家想象的那麽复杂,只需要最原始状态的数据,保存到本地即可以. 那麽,怎样才是股票数据的原始状态呢.那就看看1920's年代的道氏理论,他是怎样计算道琼斯指数,那麽他采用的 ...

  7. 吴裕雄--天生自然深度学习TensorBoard可视化:改造后的mnist_train

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...

  8. Jenkin远程部署Tomcat8.5总结

    tomcat8.5相比之前的tomcat进入manger管理界面需要多一些设置 1. 在 $tomcathome/conf/Catalina/localhost/下创建 manager.xml , 填 ...

  9. Cocoapod-终端

    安装循序: Xcode->RVM->Ruby(安装过程中需要安装homebrew)->CocoaPats 参考文章: 安装地址:http://www.cnblogs.com/dagu ...

  10. CertUtil: -hashfile 失败: 0xd00000bb (-805306181)

    使用CertUtil验证Python安装文件的时候出现了这个错误. CertUtil: -hashfile 失败: 0xd00000bb (-805306181) 代码是这样 certutil -ha ...