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

Note:
You can assume that you can always reach the last index. (Hard)

分析:

第一眼看完题目感觉用动态规划肯定能解,但是感觉就是有很多重复计算在里面...因为题目只问了个最少步数。

果然写出来之后华丽超时,但是代码还是记录一下吧

 1 class Solution {
 2 public:
 3     int jump(vector<int>& nums) {
 4         int dp[nums.size()];
 5         for (int i = 0; i < nums.size(); ++i) {
 6             dp[i] = 0x7FFFFFFF;
 7         }
 8         dp[0] = 0;
 9         for (int i = 1; i < nums.size(); ++i) {
10             for (int j = 0; j < i; ++j) {
11                 if (j + nums[j] >= i) {
12                     dp[i] = min(dp[i], dp[j] + 1);
13                 }
14             }
15         }
16         return dp[nums.size() - 1];
17     }
18 };

优化考虑类似BFS的想法,维护一个步数的范围和一个能走到的最远距离。

算法就是遍历一遍数组,到每个位置的时候,更新他能到达的最远距离end,如果一旦超过nums.size() - 1,就返回step + 1;

curEnd维护以当前步数能到达的最远范围,所以当i > curEnd时,step++,并且将curEnd更新为end。

代码:

 1 class Solution {
 2 public:
 3     int jump(vector<int>& nums) {
 4         if (nums.size() == 1) {
 5             return 0;
 6         }
 7         int step = 0, end = 0, curEnd = 0;
 8         for (int i = 0; i < nums.size(); ++i) {
 9             if (i > curEnd) {
10                 step++;
11                 curEnd = end;
12             }
13             end = max(end, nums[i] + i);
14             if (end >= nums.size() - 1) {
15                 return step + 1;
16             }
17         }
18         return -1;
19     }
20 };
时间: 2024-10-18 03:43:54

LeetCode45 Jump Game II的相关文章

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

57. Jump Game &amp;&amp; Jump Game II

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 ex

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和Jump Game II

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 ex

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

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

【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