Jump gameII

leetcode jump gameII

看了题解,用BFS是比较好的思路,一层表示当前步能到的节点,curmax表示最远的,和贪心有异曲同工之妙

class Solution {
public:
    int jump(vector<int>& a) {
        int n=a.size();
        if(n<=1) return 0;
        int i=0, level=0, curmax=0, nextmax=0;
        while(i<=n-1){
            for(;i<=curmax && i<=n-1;i++){
                nextmax=max(nextmax, a[i]+i);
                if(nextmax>=n-1) return level+1;
            }
            curmax=nextmax;
            level++;
        }
        return -1;
    }
};

之前写的dp,TLE了,当DP思维训练了

class Solution {
public:
    int jump(vector<int>& a) {
        int n=a.size();
        if(n<=1) return 0;
        int dp[n];
        dp[0]=0;
        for(int i=1;i<n;i++){
            dp[i]=INT_MAX;
            for(int j=0;j<i;j++){
                if(a[j]>=i-j)dp[i]=min(dp[i], dp[j]+1);
            }
        }
        return dp[n-1];
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 12:44:45

Jump gameII的相关文章

[LeetCode]Jump GameII

题目:Jump GameII 如果要求找最小的调数,考虑扩张的思路. 思路如下: 1.首先找起始位能到达的范围是否覆盖了最终位置,并记录下搜索中的最远能到达的位置值,即max{nums[i] + i}; 2.如果无法到达最终位置,则跳数加一,并从上一次搜索的最后位置开始,向后搜索到上一次记录的最大值所在的位置. 3.重复上面两步,直到找到最终跳数. 注意: 当数组元素只有1个时,跳数是0: 跳数的初值应该是1. int jump(vector<int>& nums){ if (nums

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

Frog Jump

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

LeetCode45 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 j

55. 45. Jump Game II *HARD*

1. 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 OJ: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

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

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

【Jump Game II 】cpp

题目: 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 j