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. PAT Advanced 1001

    1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits mu ...

  2. P3116 [USACO15JAN]会议时间Meeting Time

    P3116 [USACO15JAN]会议时间Meeting Time 题目描述 Bessie and her sister Elsie want to travel from the barn to ...

  3. mysql初始化失败的问题

    首先:my.ini 配置文件中 路径需要改成自己电脑mysql解压的路径. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...

  4. 49、android studio 使用技巧记录

    1.删除 cmd+del 2.自动导入需要的类  option+enter 3.Option + F7 ——查找哪里引用了该方 Cmd + Option + F7 —— 列出引用的列表 4.Cmd + ...

  5. leetcode 【 Rotate Image 】python 实现

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  6. php 实现栈与队列

    <?php class queueOp{ /* * 队尾入队 * Return:处理之后队列的元素个数 */ public function tailEnquque($arr,$val){ re ...

  7. Leetcode 523.连续的子数组和

    连续的子数组和 给定一个包含非负数的数组和一个目标整数 k,编写一个函数来判断该数组是否含有连续的子数组,其大小至少为 2,总和为 k 的倍数,即总和为 n*k,其中 n 也是一个整数. 示例 1: ...

  8. 聊聊、Java 命令 第二篇

    第一篇类之间没有依赖关系,所以比较简单,这一篇来看看有依赖的类怎么编译和执行. (一)Java 运行 class 有依赖 Person 是一个接口,只有一个 getName 方法.Man 则实现了 P ...

  9. PAT——乙级1032

    这些题也确实简单,但是我还是想做做,多熟悉一下C++,毕竟实践是检验真理的唯一标准,有很多小知识点自己做了才知道. 这个题是 1032 挖掘机技术哪家强 (20 point(s)) 为了用事实说明挖掘 ...

  10. [转]Docker容器内不能联网的6种解决方案

    注: 下面的方法是在容器内能ping通公网IP的解决方案,如果连公网IP都ping不通,那主机可能也上不了网(尝试ping 8.8.8.8) 1.使用--net:host选项 sudo docker ...