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. spring容器抽象的具体实现

    1.BeanFactory 接口与 ApplicationContext 接口 (1)spring 提供了两种类型的IOC容器实现.BeanFactory 和 ApplicationContext ( ...

  2. 计算机网络(6): http cookie

    Cookie作用: 1)帮助管理用户会话信息(用户需要记录的信息:登陆状态等) 2)跟踪浏览器的行为 3)用户自定义设置 实现方式: 当用户浏览带有Cookie的网站时,网站自动为其生成一个唯一的标志 ...

  3. Serverless 公司的远程团队沟通策略

    本文系译文,Serverless 团队分散在全球各地,本文介绍我们如何管理沟通策略和远程协作. 原作者:FelixDesroches 译者:Aceyclee 首先向不了解我们的人说明一下,Server ...

  4. share团队冲刺9

    团队冲刺第九天 昨天:完善代码 今天:修改代码中的问题,提高兼容性 问题:无

  5. 中小规模集群----Centos6部署wordpress及java程序

      1    概述 1.1   业务需求 公司共有两个业务,网上图书馆和一个电商网站.现要求运维设计一个安全架构,本着高可用.廉价的原则. 具体情况如下: 网上图书馆是基于jsp开发: 电商系统是基于 ...

  6. python3转义编码

    s = 'dy电影' print(s) # dy电影 print(type(s)) # <class 'str'> print(s.encode('utf-8')) # b'dy\xe7\ ...

  7. PAT Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]

    题目 Given a sequence of positive integers and another positive integer p. The sequence is said to be ...

  8. Half of UK 10-year-olds own a smartphone

    1. preposition n. 介词  pronoun  n. 代词 2. despite /preposition. (1) used to say that something happens ...

  9. Linux 笔记(自用)

    一,常用工具 1. 常用浏览器 w3m links lynx 都可以用 apt-get install *** 安装,访问方式都是 w3m/links/lynx www.baidu.com 的形式 2 ...

  10. protobuf使用遇到的坑

    在这里具体的使用我不写了,可以参考下面接个连接,我只记录自己遇到的问题. https://www.cnblogs.com/autyinjing/p/6495103.html(此博客很详细,不过最好不要 ...