55. 跳跃游戏

问题描述

  1. 给定一个非负整数数组,你最初位于数组的第一个位置。
  2. 数组中的每个元素代表你在该位置可以跳跃的最大长度。
  3. 判断你是否能够到达最后一个位置。
  4. 示例 1:
  5. 输入: [2,3,1,1,4]
  6. 输出: true
  7. 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 3 步到达最后一个位置。
  8. 示例 2:
  9. 输入: [3,2,1,0,4]
  10. 输出: false
  11. 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 所以你永远不可能到达最后一个位置。

代码1(回溯法)

  1. class Solution {
  2. public:
  3. bool canJump(vector<int>& nums) {
  4. int n = nums.size();
  5. return Jump(nums,0,n);
  6. }
  7. bool Jump(vector<int>& nums,int position,int n)
  8. {
  9. if(position == n-1)return true;
  10. else if(position > n-1)return false;
  11. int furthermove = min(n-1,position+nums[position]);
  12. //for(int i = position+1; i <= furthermove; i++)
  13. for(int i = furthermove; i > position; --i)
  14. {
  15. if(Jump(nums,i,n))return true;
  16. }
  17. return false;
  18. }
  19. };

但是该方法在74/75测试用例超出时间限制。

代码2(剪枝回溯/自顶向下)

  1. class Solution {
  2. public:
  3. bool canJump(vector<int>& nums) {
  4. int n = nums.size();
  5. vector<int> flag;
  6. flag.resize(n,-1);
  7. flag[n-1] = 1;
  8. return Jump(nums,0,n,flag);
  9. }
  10. bool Jump(vector<int>& nums,int position,int n,vector<int>& flag)
  11. {
  12. if(flag[position] == 1){
  13. return true;
  14. }
  15. else if(flag[position] == 0) return false;
  16. int furthermove = min(n-1,position+nums[position]);
  17. //for(int i = position+1; i <= furthermove; i++)
  18. for(int i = furthermove; i > position; --i)
  19. {
  20. if(Jump(nums,i,n,flag))
  21. {
  22. flag[i] = 1;
  23. return true;
  24. }
  25. }
  26. flag[position] = 0;
  27. return false;
  28. }
  29. };

但遗憾的是该方法在74/75测试用例仍然超出时间限制。

代码3(自底向上)

  1. class Solution {
  2. public:
  3. bool canJump(vector<int>& nums) {
  4. int n = nums.size(),i,j;
  5. vector<int> flag;
  6. flag.resize(n,-1);
  7. flag[n-1] = 1;
  8. for(i = n-2; i > -1; --i)
  9. {
  10. int furthermove = min(n-1,i + nums[i]);
  11. for(j = i+1; j <= furthermove; ++j)
  12. {
  13. if(flag[j] == 1)
  14. {
  15. flag[i] = 1;
  16. break;
  17. }
  18. }
  19. }
  20. return flag[0] == 1;
  21. }
  22. };

结果仍然不是很理想:

  1. 执行用时 :696 ms, 在所有 C++ 提交中击败了13.81%的用户
  2. 内存消耗 :15 MB, 在所有 C++ 提交中击败了5.04%的用户

代码4(贪心法)

用贪心来做,设置变量furthermove表示当前所能达到的最远的位置,那么状态转移方程为furthermove = max(furthermove, nums[i] + i),括号里的furthermove是上一步的最优解,因此是贪心。当到了某一个点 i>furthermove 的时候,说明已经走不到这一点了。

  1. class Solution {
  2. public:
  3. bool canJump(vector<int>& nums) {
  4. int n = nums.size(),i,furthermove = nums[0];
  5. for(i = 1; i < n && i<= furthermove; ++i)
  6. {
  7. // if(i <= furthermove)
  8. furthermove = max(furthermove,i+nums[i]);
  9. }
  10. return furthermove>=n-1;
  11. }
  12. };

结果:

  1. 执行用时 :16 ms, 在所有 C++ 提交中击败了34.84%的用户
  2. 内存消耗 :14.7 MB, 在所有 C++ 提交中击败了5.04%的用户

45. 跳跃游戏 II

问题描述

  1. 给定一个非负整数数组,你最初位于数组的第一个位置。
  2. 数组中的每个元素代表你在该位置可以跳跃的最大长度。
  3. 你的目标是使用最少的跳跃次数到达数组的最后一个位置。
  4. 示例:
  5. 输入: [2,3,1,1,4]
  6. 输出: 2
  7. 解释: 跳到最后一个位置的最小跳跃数是 2
  8.   从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
  9. 说明:
  10. 假设你总是可以到达数组的最后一个位置。

问题分析

  • 1、如果某一个作为 起跳点 的格子可以跳跃的距离是 3,那么表示后面 3 个格子都可以作为 起跳点;可以对每一个能作为 起跳点 的格子都尝试跳一次,把 能跳到最远的距离furthermove 不断更新。
  • 2、首先从第0个元素开始,边界end是0,可以跳到的最远距离2,可以跳到1(i =1,furthermove就是4),也可以跳到2(i= 2,furthermove就是3),但是只有两个选择,贪心算法的要素就是获取当前阶段最优解;显然应该跳到1,此时边界end是4。遍历数组的时候,到了边界,我们就重新更新新的边界。
  • 3、如果furthermove的值大于等于len-1,那么就可以一直跳到最后,就成功了。

代码

  1. class Solution {
  2. public:
  3. int jump(vector<int>& nums) {
  4. int n = nums.size(),i,furthermove=0,end=0,step=0;
  5. if(n == 1)return 0;
  6. for(i = 0; i < n; i++)
  7. {
  8. furthermove = max(furthermove,nums[i]+i);
  9. if(furthermove>=n-1)return ++step;
  10. if(i == end)
  11. {
  12. end = furthermove;
  13. ++step;
  14. }
  15. }
  16. return step;
  17. }
  18. };

问题分析:

  1. 执行用时 :12 ms, 在所有 C++ 提交中击败了73.08%的用户
  2. 内存消耗 :15.3 MB, 在所有 C++ 提交中击败了5.09%的用户

leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II的更多相关文章

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

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

  2. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

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

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

  4. LeetCode 45. 跳跃游戏 II | Python

    45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...

  5. 力扣Leetcode 45. 跳跃游戏 II - 贪心思想

    这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...

  6. Java实现 LeetCode 45 跳跃游戏 II(二)

    45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  7. [leetcode] 45. 跳跃游戏 II(Java)(动态规划)

    45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...

  8. [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ

    跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...

  9. Unity 2D游戏开发教程之游戏中精灵的跳跃状态

    Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...

随机推荐

  1. CF749A Bachgold Problem 题解

    Content 给定一个数 \(n\),求它最多能够拆分成多少个质数,并输出拆分成的每一个质数. 数据范围:\(2\leqslant n\leqslant 10^5\). Solution 我们考虑尽 ...

  2. WebSocket协议理解-数据包格式解析

    WebSocket 的诞生 做客户端开发时,接触最多的应用层网络协议,就是 HTTP 协议,而今天介绍的 WebSocket,下层和 HTTP 一样也是基于 TCP 协议,这是一种轻量级网络通信协议, ...

  3. python开发环境软件包安装相关 failed with error code 1 in /tmp/pip-build-vn_f_e1n/psutil/

    指定源安装 pip install git+https://github.com/xxxxxx.git pip install -r requirements.txt -i https://mirro ...

  4. layui踩过的坑

    layui 表格合计行取整 原代码:(文件位置:..\layui\lay\modules\table.js) var e=o.totalRowText||"",i=parseFlo ...

  5. JAVA携带参数(带有请求参数,请求头参数)直接发送POST请求

    <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...

  6. 一个VS主题网站https://studiostyl.es/

    地址: https://studiostyl.es/ 用法: 工具->导入和导出设置->导入选定的环境设置->是,保存我当前的设置->选择下载的主题文件,完成.

  7. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  8. 【LeetCode】66. Plus One 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数九 采用进位 日期 [LeetCode] 题目地址 ...

  9. 【九度OJ】题目1475:IP数据包解析 解题报告

    [九度OJ]题目1475:IP数据包解析 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1475 题目描述: 我们都学习过计算机网络, ...

  10. 【LeetCode】45. Jump Game II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...