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

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

Hide Tags

Array Greedy


这题是前面一题的变种,其实用的是贪心算法,不过数据设置了时间限制,所以需要提交效率,最好就是一次O(n)时间。

为了提高时间需要做一个思考,如下面位置,是否存在 i-th 需要跳的最少次数少于i-1 th 位置,如

... i-1 i
... 3 2

  这个可以这么思考,如果i 是从i-1 跳到的,那么i位置的最少跳数 <i> = <i-1> +1.

如果i 是从i-2  或之前跳到的,那么<i> = <i-1>.

可以知道必定有<i>   >=   <i-1>。

利用这个性质可以加快速度。

算法逻辑:

  1. 创建等长的字符串a,a[i] 表示到i-th  的最少跳数,有a[0]等于0,结果就是a[n-1]。
  2. 创建最大已知位置索引maxIdx,表示在i-th 位置时,已经最远确定的位置索引,初始化为1,即指向未知的位置最小的。
  3. 遍历数组。
  4. 如果遍历位置加上跳跃长度大于等于maxIdx,那么更新数组a 到最大索引maxIdx
  5. 如果maxIdx ==n 跳出
  6. 返回a[n-1].

这样遍历的时间其实只有n 次,时间O(n),空间也是O(n).改进地方就是空间改O(1)。

 1 #include <iostream>
 2 using namespace std;
 3
 4 class Solution {
 5 public:
 6     int jump(int A[], int n) {
 7         int *a = new int [n];
 8         for(int i=1;i<n;i++)
 9             a[i]=INT_MAX;
10         a[0]=0;
11         int maxidx = 1;
12         for(int i=0;i<n;i++){
13             while(maxidx<=i+A[i]&&maxidx<n){
14 //            for(int kk=0;kk<n;kk++) cout<<a[kk]<<" ";
15 //        cout<<endl;
16                 a[maxidx++] = a[i]+1;
17             }
18         }
19         int ret = a[n-1];
20         delete []a;
21         return ret;
22     }
23 };
24
25 int main()
26 {
27     int a[]={2,3,1,1,4};
28     Solution sol;
29     cout<<sol.jump(a,sizeof(a)/sizeof(int))<<endl;
30     return 0;
31 }

discuss 中有空间O(1)的实现,逻辑是一样的,就贴上来吧。

https://oj.leetcode.com/discuss/422/is-there-better-solution-for-jump-game-ii

 1 class Solution {
 2 public:
 3     int jump(int A[], int n) {
 4         int ret = 0;
 5         int last = 0;
 6         int curr = 0;
 7         for (int i = 0; i < n; ++i) {
 8             if (i > last) {
 9                 last = curr;
10                 ++ret;
11             }
12             curr = max(curr, i+A[i]);
13         }
14
15         return ret;
16     }
17 };

时间: 2024-12-28 18:57:46

[LeetCode] Jump Game II 贪心的相关文章

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

[leetcode]Jump Game II @ Python

原题地址:https://oj.leetcode.com/problems/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 i

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

[LeetCode]Jump Game II (贪心,维护当前最远能到达的位置和所需最少步数)

最少跳跃步数 第一想法是DP,复杂度O(n^2),但是对于大型数据会超时. Discuss中一种犀利的贪心方法,复杂度为O(n) class Solution { public: int jumpDP(int A[], int n) {//DP方法 int *dp=new int[n],j; memset(dp,127,sizeof(int)*n); dp[0]=0; int i=0; for(i=0;i<n-1;++i){ for(j=1;j<=A[i]&&i+j<n;

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

【To Read】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 j

【贪婪算法、动态规划】Jump Game II

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