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.

问题:给定一个数字,假设你站在第一个元素,每个元素表示你可以跳得最大距离。求你是否可以跳到最后一个元素。

思路一,假设已知 A[i+1...n] 是否可以跳都终点(最后一个元素),则 A[i] 是否可以跳到终点只需要检查 A[i+k] 是否可以跳到终点即可。

这实际是一个 DP 思路。对于每个 A[i] 都可能检查大概 (n-i) 次,时间复杂度为 O(n*n),超时。

思路二,结合思路一以及超时的  test case 发现,其实对于 A[i] 无需检查 k 次,只需要检查 A[i] 和右边最近的可达终点的元素的距离是否小于 A[i] 值即可。

v[i] 表示在 nums[i...n]中,nums[i] 到达下一个可达终点元素的距离。

例如,nums[i]自身可达终点,则v[i] = 0;nums[i]不可达而nums[i+1]可达,则 v[i] = 1;nums[i]不可达而nums[i+k]才可达,则v[i] = k;

借组辅助表格 v ,A[i] 只需要检查 v[i+1] 是否小于 A[i] 就可以知道 A[i] 是否可达终点。

元素是否可达终点,所以只需要判断 A[i] 的最大可跳距离是否大于最近可行元素即可,最近可行元素之后的情况无需考虑。思路二 实际就是贪心思路(Greedy),题目也是归类到 Greedy 下面的,吻合。

     bool canJump(vector<int>& nums) {

         if (nums.size() < ) {
return true;
} vector<int> v(nums.size()); long len = nums.size();
if (nums[len - ] > ) {
v[len - ] = ;
}else{
v[len - ] = ;
} for (int i = (int)nums.size()-; i >= ; i--) {
if (nums[i] > v[i+]) {
v[i] = ;
}else{
v[i] = v[i+] + ;
}
} return (v[] == ) ? true : false;
}

[LeetCode] 55. Jump Game 解题思路的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] 134. Gas Station 解题思路

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  3. LeetCode: 55. Jump Game(Medium)

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

  4. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. 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 ...

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

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

  8. 【LeetCode】55. Jump Game 解题报告(Python & C++)

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

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

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

随机推荐

  1. android如何让service不被杀死

    1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建 @Override     public int onStartCom ...

  2. C#中将图片文件转化为二进制数组-用于数据库存储

    在项目开发中,使用SQL Server存储数据,数据类型image可以保存图片.但是在存储之前需要将图片转化为二进制数组的形式进行赋值. 将图片文件转换为二进制数组 /// <summary&g ...

  3. javascript:运动框架

    function startMove(obj,json,fnEnd) { clearInterval(obj.timer);//清除定时器 obj.timer=setInterval(function ...

  4. css position 应用(absolute和relative用法)

    1.absolute(绝对定位) absolute是生成觉对定位的元素,脱离了文本流(即在文档中已经不占据位置),参照浏览器的左上角通过top,right,bottom,left(简称TRBL) 定位 ...

  5. (转)SQL流程控制语句学习(二):begin…end if…else case

    1.begin…end 语法: begin {sql语句或语句块} end 注意:begin 和end要成对使用 2.if…else 语法: if  布尔表达式 {sql语句或语句块} else  布 ...

  6. ngrok内网穿透(微信调试:只试用于微信测试账号)

    一.简介 ngrok:https://ngrok.com 功能:就是把外网地址映射到本地的内网地址 缺点: 1.免费版生成的域名是随机的(由于我是用于调试,就没什么关系,如果是正式生产环境可能需要一个 ...

  7. excel导出的集中情况

    jsp 页面: 导出按钮: <form id="excel" name="exportForm" method="post" acti ...

  8. Java中的List(转)

    List包括List接口以及List接口的所有实现类.因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以Li ...

  9. 多线程08-Callable和Future

    1.简介 Callable是一个接口,与Runnable类似,包含一个必须实现的call方法,可以启动为让另一个线程来执行,执行Callable可以得到一个Future对象 该对象可以监听Callab ...

  10. wm_char

    用于接收键盘输入的消息 int CXuexi2View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateS ...