zoukankan      html  css  js  c++  java
  • [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 }
    View Code
    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 };
    View Code
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    项目笔记:导出Excel功能设置导出数据样式
    前后台JSON传值得一个问题和异常处理net.sf.json.JSONException: Unquotted string '"name"'
    ES6中的async函数
    zoj 1203 Swordfish
    C#:excel导入导出
    &quot;undefined reference to strptime&quot;之自己定义strptime函数
    OpenGL蓝宝书第七章:立体天空和纹理折射、双纹理(下)
    HDOJ 5384 Danganronpa AC自己主动机
    Hibernate复习之Hibernate基本介绍
    Java集合---ConcurrentHashMap原理分析
  • 原文地址:https://www.cnblogs.com/Azhu/p/4141896.html
Copyright © 2011-2022 走看看