leetcode-55. Jump Game - Medium

descrition

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.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false.

解析

这是一道动态规划的题,官网有很详细的解答leetcode-solution

动态规划算法的设计步骤:

  1. 递归回溯求解(自顶向下)
  2. 使用记忆回溯法求解 (自顶向下的动态规划)
  3. 去掉递归,使用自底向上的动态规划
  4. 进一步使用技巧优化自底向上的动态规划

根据以上步骤得到以下的解决方法。代码只给出最有解,其他可参考官网的 solution。

方法 1 :

回溯法,recursive(vector& nums, int icur)

  • 如果 icur == nums.size() - 1,到达最后一个位置,递归的出口,表示可以到达最后,返回 true。
  • 递归:i, 1+icur <= i <= min(icur+nums[icur], n-1)

即对每一种情况都递归检查一遍。

时间复杂度-O(2^n); 空间复杂度-O(n),递归需要额外的空间。

方法 2

实际上就是在方法 1 的基础上系上一个 memory 数组,表示当前访问下标 icur 的状态,这样可以避免重复计算。

递归函数可以定义为:recursive(vector& nums, int icur, vector& memory)

时间复杂度-O(n^2),n 个元素,没个元素最多有 n 种可能;空间复杂度-O(n),递归需要额外空间。

方法 3

方法 1 和 方法 2 在对每一步进行试探时从近到远。还可以考虑从远到近进行检查,即每次都从可能到大的最远处进行检查。这种方法的时间复杂度并没有优化,但是从实践的角度可以进行一定的优化。

方法 4

动态规划方法。假设数组 dp[i] 表示位置 i 的状态:UNKONW, GOOD, BAD,分别表示未知,可达,不可达状态。

那么对于任意位置 i ,检查所有的 j = [i+1, min(i+nums[i], n-1)] 的状态,只要有一个 dp[j] = GOOD,则说明 dp[i] 也是 GOOD。边界条件 dp[n-1] = GOOD。

最后我们只需要检查 dp[0] 是否等于 GOOD 即可。

方法 5

对动态规划的进一步优化。核心思想贪心。我们可以从两个角度来看。最优解,时间复杂度-O(n),空间复杂度-O(1)。

1. 从右往左遍历

令 lastPos 表示当前可以到达的最靠近左边的位置。从右边往左边遍历:for(int i=n-1; i>=0, i--)

对任意 i,最远可到达的位置为 farthest = i + nums[i],如果 farthest >= lastPos,则说明从当前位置可以到达最尾的位置,此时更新 lastPos = i 。

最后检查 lastPos 是否为 0 即可。

2. 从左往右遍历

令 farthest 表示能到达的最远距离,我们从左往右遍历:for(int i=0; i<n; i++)。直观来说就是我们每次都往前移动一步,如果 i > farthest 则说明不可能到达最尾节点,直接返回 false。循环中每次都更新 farthest = max(i+nums[i], farthest)。

如果循环能顺利结束,则说明可以到达最尾节点。

code

#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
bool canJump(vector<int>& nums){
//return canJumpRight2Left(nums);
return canJumpLeft2Right(nums);
} bool canJumpRight2Left(vector<int>& nums){
int lastPos = nums.size() - 1;
for(int i=nums.size()-1; i>=0; i--){
if((i + nums[i]) >= lastPos){
lastPos = i;
}
}
return lastPos == 0;
} bool canJumpLeft2Right(vector<int>& nums){
int farthest = 0;
for(int i=0; i<nums.size(); i++){
if( i > farthest)
return false;
farthest = max(i + nums[i], farthest);
} return true;
}
}; int main()
{
return 0;
}

[array] leetcode-55. Jump Game - Medium的更多相关文章

  1. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

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

  2. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

  3. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  4. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  5. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  6. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  7. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  8. [leetcode] 55. Jump Game (Medium)

    原题 题目意思即 每一格代表你当前最多能再往后跳几次,从第一格开始,如果能跳到最后一格返回true,反之为false. 思路:用一个下标记录当前最多能跳到哪一格,遍历一遍 --> 如果当前格子不 ...

  9. [LeetCode] 55. Jump Game 跳跃游戏

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

  10. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

随机推荐

  1. 如何用while循环 输出一个九九乘法表

    方法一 i = 1 while i < 10: k = 1 while k <= i: print('%d*%d=%2d '% (i,k,i*k),end='') #end='' 表示不换 ...

  2. km算法入门

    本文知识均由笔者自学,文章有错误之处请不吝指出. 笔者刷数模题的时候有一道题考到了"二分图最大权分配",需要用到KM算法,但是书上对KM算法的介绍又臭又长,更何况有些同学" ...

  3. Apache服务器配置

    之前做代码一直按照传统化的方法部署别人的网站,但是一直不成功,尝试了很多次最后才发现时虚拟主机的问题 使用apache默认为127.0.0.1和网站的配置发生冲突. 因此在apache的conf文件夹 ...

  4. MYSQL瓶颈

    一般来说, 在使用的时候 性能从某个数值开始.突然大大下降,说明就到了瓶颈期. mysql 瓶颈有2种,一种是 cpu瓶颈  一种是 io瓶颈.cpu瓶颈多是由io引起. 而io可以通过  show ...

  5. float和double的区别

    1.float是单精度类型,精度有效数字为6位,超出则会四舍五入,取值范围为10的-38次方到10的38次方,float占用存储空间为4个字节. 2.double是双精度类型,精度有效数字为15位,超 ...

  6. echarts分组插件echarts.group代码分享

    前言 echarts是百度出品的一款很棒的前端图表控件,被评为"百度少有的良心产品".可以实现散点图.折线图.柱状图.地图.饼图.雷达图.K线图等等几十种常用.不常用的图表,效果酷 ...

  7. vue init失败解决方案-终极版

    //由于windows系统的某方面问题,vue脚手架安装可能会出现第一证书丢失 // 报错:vue-cli · Failed to download repo vuejs-templates/webp ...

  8. Git 二分调试法,火速定位疑难Bug!

    你一定遇到过,一个很久没修改过的功能,莫名其妙的出现了问题?肉眼查代码.屡逻辑完全找不到问题点?前两天还好好的功能,怎么这个今天就不行了?这两天改动了这么多代码,到底是那一次改动引发的 Bug? 这样 ...

  9. 《iOS Human Interface Guidelines》——Multitasking

    多任务处理 多任务处理让人们在屏幕上(以及合适的iPad模式)查看多个app,而且在近期使用的app中高速地切换. 在iOS 9中.人们能够使用多任务处理UI(例如以下所看到的)来选择一个近期使用的a ...

  10. Linux学习记录--文件IO操作相关系统编程

    文件IO操作相关系统编程 这里主要说两套IO操作接口,各自是: POSIX标准 read|write接口.函数定义在#include<unistd.h> ISO C标准 fread|fwr ...