Leetcode:Jump Game 跳跃楼梯

Jump Game

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.

Determine if you are able to reach the last index.

For example:

A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

解题分析:

每一层最大可以跳 A[i] 步,当然可以 跳 0,1,2...A[i]中的任意步

我们可以 从下表0出发,每一层的时候我们都依次跳 1,2,3...A[i]步,并且每跳一步,我们就更新 可以达到的最到楼层

如果最后能超过 最高层,说明能够达到

class Solution {
public:
    bool canJump(int A[], int n) {
        assert(A != NULL && n >= 0);
        int reachIndex = 0;
        for (int i = 0; i <= reachIndex && reachIndex < n; ++i) {
            reachIndex = max(reachIndex, i + A[i]);
        }
        return reachIndex >= n - 1;
    }
};

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.

For example:
Given array A = [2,3,1,1,4]

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

解题分析:

此题需要求解 最小步数

我们每次尝试 从每一层跳最远

例如,假设 [left, right] 是当前跳跃能覆盖的区间

我们从left到right遍历,我们查找 从[left, right]区间任一楼层 能够 跳到的 最高楼层,如果 可以达到的最高楼层 >= 实际的最高楼层,立即返回

否则,我们 更新为 left = oldRight + 1, 即 下一步 从 原来的right+1 开始跳,因为 原来的right代表了 这一步可以到达的最高楼层

class Solution {
public:
    int jump(int A[], int n) {
        assert(A != NULL && n >= 0);
        if (n == 1) return 0;
        int step = 0;
        int left = 0;
        int right = 0;

        while (left <= right) {
            ++step;
            const int rightTmp = right;
            for (int i = left; i <= rightTmp; ++i) {
                int newRight = i + A[i];
                if (newRight >= n - 1) return step;
                if (newRight > right) right = newRight;
            }
            left = rightTmp + 1;
        }
        return 0;
    }
};

Leetcode:Jump Game 跳跃楼梯,布布扣,bubuko.com

时间: 2024-10-12 12:25:16

Leetcode:Jump Game 跳跃楼梯的相关文章

[Leetcode] jump game 跳跃游戏

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. Determine if you are able to reach the last index. For example:A =[

[LeetCode] 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

[LeetCode] 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

LeetCode: Jump Game II [044]

Perface 如果让你实现这个页面和一些操作的,比如点击1.2.3等就在那个input text中显示,还有删除功能,拨打我们先不要管它,只是模拟而已.要是我刚开始做的话,我会这样做: 用css.HTML布局那个界面 用javascript的事件委托监听那个按钮的父节点的点击事件 但是如果我想用面向对象的思想做呢?我是用Ext做的,所以我想说的是它帮我封装了很多.可能一些没用过Ext的人不太了解我下面贴的代码,但是我会尽量解释清楚的! Description ContactTelPanel =

Leetcode | Jump Game I &amp;&amp; II

Jump Game I 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. Determine if you are able to reach the last index. For

LeetCode: Jump Game [054]

[题目] 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. Determine if you are able to reach the last index. For example

[leetcode]Jump Game II @ Python

原题地址:https://oj.leetcode.com/problems/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 i

LeetCode 55 _ Jump Game 跳跃游戏

Description: 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. Determine if you are able to reach the last index. Exa

【LeetCode每天一题】Jump Game(跳跃游戏)

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.Determine if you are able to reach the last index. Example 1: Input: