https://oj.leetcode.com/problems/jump-game-ii/

给一个数列,每次可以跳相应位置上的步数,问跳到最后位置至少用几步。

动态规划:

j[pos]表示从0到pos至少要跳的步数,初始化为n

j[pos] = min { j[i] + 1 ,j[pos]}  if(A[i] + i >=pos)  i 从0到pos

这属于一维动态规划

class Solution {
public:
int jump(int A[], int n) {
if(n == )
return ; vector<int> ans;
ans.resize(n);
ans[] = ; for(int i = ;i<n;i++)
{
ans[i] = n;//initialize for(int j = ;j<i;j++)
{
if(A[j] + j >= i)
ans[i] = min(ans[i],ans[j] + );
}
}
return ans[n-];
}
};

复杂度为O(n*n)超时了。

于是看了答案,用贪心,类似宽度优先搜索的概念。

维护一个当前范围(left,right)表示从当前范围经过一步可以调到的范围(min_distance,max_distance)。

class Solution {
public:
int jump(int A[], int n) {
if(n == )
return ; if(n==)
return ; int ans_step = ;
int left = , right = ; int max_distance = ;
int min_distance = n;
while()
{
ans_step++;
max_distance = ;
min_distance = n;
int i;
for(i = left; i <= right && i< n;i++)
{
int this_time = i + A[i]; if( this_time > max_distance)
max_distance = this_time; if(max_distance >= n-)
return ans_step;
}
if(i == n)
break; i = left;
bool flag = false;
while(i<=right)
{
//find the first number not 0
if(A[i]!=)
{
min_distance = i + ;
flag = true;
break;
}
i++;
}
if(flag == false)
break;
right = max_distance;
left = min_distance; }
return n;
}
};

LeetCode OJ-- Jump Game II **的更多相关文章

  1. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  2. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

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

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [LeetCode] 45. Jump Game II 跳跃游戏 II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. LeetCode 045 Jump Game II

    题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...

  6. [LeetCode] 45. Jump Game II 解题思路

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

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

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  8. [LeetCode] 45. Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. Leetcode 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  10. Java for LeetCode 045 Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. python 斗图图片爬虫

    捣鼓了三小时,有一些小Bug,望大佬指导 废话不说,直接上代码: #!/usr/bin/python3 # -*- coding:UTF-8 -*- import os,re,requests fro ...

  2. 指针的操作 p*++

    int x, y, *px = &x, *py = &y; y = *px + ; //表示把x的内容加5并赋给y,*px+5相当于(*px)+5 y = ++*px; //px的内容 ...

  3. HDU 2087 HDU 1867 KMP标准模板题

    贴两道题,其中HDU2087是中文题,故不解释题目, 思路是,一发KMP,但是特别处理最后一位的失配边为0,这样就可以保证“判断完成但是不多判断”. 第二题,很毒瘤的题,要求求出,给定字符串A,B能够 ...

  4. lua table长度解析

    先来看lua table源码长度获取部分(ltable.c) j是数组部分的长度.首先判断数组长度大于0,并且数组最后一个是nil,就用二分法查找,返回长度. 如果t->node是 table的 ...

  5. HDU 5739 Fantasia 双连通分量 树形DP

    题意: 给出一个无向图,每个顶点有一个权值\(w\),一个连通分量的权值为各个顶点的权值的乘积,一个图的权值为所有连通分量权值之和. 设删除顶点\(i\)后的图\(G_i\)的权值为\(z_i\),求 ...

  6. 常用的一些api

    发送手机短信 // 发送短信给安全号码 SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phon ...

  7. Ping过程&ICMP

    1.ICMP(Internet控制消息协议) ICMP=Internet Control Message Protocol 它是TCP/IP协议族的一个子协议 作用:用于在IP主机.路由之间传递控制消 ...

  8. 扩展MarkDown表格

    一直不知道表格中的:是什么意思,看了GcsSloop的这篇文章后恍然大悟,做下记录. 原文链接 第二行分割线部分可以使用 : 来控制内容状态 MarkDown : | 默认 | 靠右 | 居中 | 靠 ...

  9. Java之基于Apache jar包的FTPClient上传

    首先,准备工作: http://pan.baidu.com/s/1dD1Utwt 从以上链接下载Apache的jar包,并将其复制到工程的WEB-INF下的lib包里,在此,准备工作就已经完成了. 具 ...

  10. IOS开发---菜鸟学习之路--(二)-数据获取

    第二篇了. 本篇要讲的是数据获取. 为什么将数据获取放在第二篇就讲呢? 因为我在看别人教程的时候都是先讲控件的属性,然后怎么用控件开始. 可是毕竟咱们也是有一定开发经验的人..所以很自然就想先知道怎么 ...