leetcode || 55、Jump Game

problem:

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.

Hide Tags

Array Greedy

题意:从起点跳跃,每次跳跃的最大步数为该位置的数组值,判断能否最后跳到最后一个位置

thinking:

(1)刚开始想到DFS,对每一个到达的结点作深搜,但是由于边界条件较少,时间复杂度会急剧膨胀,不太可行

(2)另外一种方法是采用贪心策略,之前一道Jump Game 题目,求步数的也是采用贪心。思路是,采用A[base+step]+step的加权方式来衡量每一次选择的优劣,判断当跳到第一个0处,能否到达或者超过数组最后一个数字。

这里假设成立的是:每次跳跃选择往最远处跳跃,如果最后能够跳到数组最后一位或者最后一位之后,那么一定存在一种跳跃方式正好跳到最后一位上

这个假设很容易证明是成立的,因为最后一步跳跃可以选择0~step之间的任意一步

code:

class Solution {
public:
    bool canJump(int A[], int n) {
        if(n==1)
            return true;
        int index=0;
        int _max=0;
        while(index<n)
        {
            if(_max>=n-1)
                return true;
            if(A[index]==0 )
            {
                if(_max<n-1)
                    return false;
                else
                    return true;
            }
            int tmp=A[index];
            int flag=0;
            for(int i=1;i<=A[index];i++)
            {
                if(A[index+i]+i>=tmp)
                    {
                        tmp=A[index+i]+i;
                        flag=i;
                    }
            }
           index+=flag;
           _max=A[index]+index;
        }

    }
};
时间: 2024-07-31 19:57:22

leetcode || 55、Jump Game的相关文章

BFS问题-LeetCode 55、45、5297、127、433、434(BFS)

[LeetCode #55]跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置. class Solution { public: bool canJump(vector& nums) { int maxReach = nums[0];

LeetCode 55/45 Jump Game I/II-----Greedy**

一: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.

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#55, 45]Jump Game, Jump Game II

The problem: 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 || 45、 Jump Game II

problem: 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

leetcode || 118、Pascal&#39;s Triangle

problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

leetcode || 119、Pascal&#39;s Triangle II

problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

[LeetCode]55.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:

LeetCode 55. 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 =