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. Ubuntu samba 安装与配置 实现windows和虚拟机中的Ubuntu共享文件

    2.    安装sumba服务 sudo apt-get install samba samba-common 这里出现了小问题, Ubuntu上安装samba不能安装的问题,“下列的软件包有不能满足 ...

  2. <html5 canvas>一个简单的矩形

    Html5: <!doctype html> <html> <head> <meta charset="UTF-8"> <ti ...

  3. poj 2533Longest Ordered Subsequence

    Longest Ordered Subsequence Description A numeric sequence of ai is ordered if a1 < a2 < - < ...

  4. LOJ #6008. 「网络流 24 题」餐巾计划

    #6008. 「网络流 24 题」餐巾计划 题目描述 一个餐厅在相继的 n nn 天里,每天需用的餐巾数不尽相同.假设第 i ii 天需要 ri r_ir​i​​ 块餐巾.餐厅可以购买新的餐巾,每块餐 ...

  5. 创建OpenStack的存储云

    OPENSTACK内部 OpenStack是一个开源的云平台项目,是由NASA发起,Rackspace在2010作为一个项目进行主导.源代码是由OpenStack基金会管理并在准许Apache许可下发 ...

  6. JS空数组的判断

    前言 最近在做一个mini项目,被大神各种鄙视,基础知识确实是不扎实,加油加油.好了,不多废话,抽空写写遇到的两个知识点,就记录下来,写博客还是能帮忙整理记录的,不然过了就忘记了. input监听值改 ...

  7. Python基础-week07 Socket网络编程

    一 客户端/服务器架构 1.定义 又称为C/S架构,S 指的是Server(服务端软件),C指的是Client(客户端软件) 本章的中点就是教大写写一个c/s架构的软件,实现服务端软件和客户端软件基于 ...

  8. URIs, URLs, and URN

    首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体 ...

  9. 整理 pycharm console调试博客

    在Debug模式下,查看变量发现只能看到300个变量,报错: two large to show contents. Max items to show:300. 点击Debugger左侧consol ...

  10. HLG 1494网络 (求的割点数目)可做模板

    网络 Time Limit: 1000 MS Memory Limit: 65535 K Total Submit: 103(32 users) Total Accepted: 54(31 users ...