[LeetCode] #45 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.)

本题是跳跃问题,利用动态规划求解,每次记录最远到达的距离和步数。时间:16ms。代码如下:

class Solution {
public:
    int jump(vector<int>& nums) {
        size_t n = nums.size();
        if (n == 0 || n == 1)
            return 0;
        int maxstep = nums[0], step = 1;
        if (maxstep >= n - 1)
            return 1;
        for (int i = 1; i < n; i++){
            if (maxstep == 0)
                return -1;
            maxstep--;
            if (maxstep < nums[i]){
                maxstep = nums[i];
                step++;
            }
            if (maxstep + i >= n - 1)
                return step;
        }
        return -1;
    }
};
时间: 2024-07-29 14:24:03

[LeetCode] #45 Jump Game II的相关文章

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

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 nu

LeetCode 45 Jump Game II(按照数组进行移动)

题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. 求解出从下标为0开始到下标到数组最后一个所需要的最少跳动次数! 1.当数组为空或者数组长度等于1时,不需要跳动,因此返回0 否则初始化step=1 2.初始化left=0 right = nums[0] 当left<=right时进入循环体中. 3.从第i=0开始,如果当前跳动距离大于数组长度则返回st

leetcode 45 Jump Game II ---- java

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

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

45. Jump Game II(js)

45. 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 minimu

LeetCode 045 Jump Game II

题目要求: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 minim

55 Jump Game i &amp;&amp; 45 Jump Game ii

Jump Game Problem statement: 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

Leetcode 贪心 Jump Game II

Jump Game II Total Accepted: 16242 Total Submissions: 65802My Submissions 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 pos