LeetCode OJ:Jump Game II(跳跃游戏2)

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

如题,求到达末位最少的跳的次数。

 1 class Solution {
 2 private:
 3     int dp[100000];
 4 public:
 5     int jump(vector<int>& nums) {
 6         int maxPos = 0;
 7         dp[0] = 0;
 8         int pos = 0;
 9         int sz = nums.size();
10         for(int i = 0; i <= maxPos; ++i){
11             pos = i + nums[i];
12             if(pos >= sz)
13                 pos = sz - 1;
14             if(pos > maxPos){
15                 for(int j = maxPos + 1; j <= pos; ++j)
16                     dp[j] = dp[i] + 1;
17                 maxPos = pos;
18             }
19
20             if(maxPos == sz - 1)
21                 return dp[sz - 1];
22         }
23
24     }
25 };
时间: 2024-10-23 05:15:09

LeetCode OJ:Jump Game II(跳跃游戏2)的相关文章

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每天一题】Jump Game II(跳跃游戏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 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 = 

【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(45): 跳跃游戏 II

Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2.   从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置. 说明: 假设你总是可以到达数组的最后一个位置. 解题思路: 这题是之前那道Jump Game 跳跃游戏 的延伸,那题是问能不能

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

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

【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

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 =