[LeetCode] 55. Jump Game 解题思路
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 解题思路的更多相关文章
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [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 ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
- [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 ...
- [LeetCode] 53. Maximum Subarray 解题思路
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 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 ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- 【LeetCode】55. Jump Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
随机推荐
- Java基础知识强化79:被遗忘的Java Math类
1. Math类概述 Math类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 2. 成员变量 和 成员方法(常用的) (1)成员变量 public static final d ...
- Java设计模式---组合模式
一.组合模式定义 组合模式定义: Compose objects into tree structures to represent part-whole hierarchies. Composite ...
- 安装android studio 出现的路径问题 tools.jar' seems to be not in Android Studio classpath
尝试一下android studio ,谁知出现路径问题 'tools.jar' seems to be not in Android Studio classpath. Please ensure ...
- codevs 4827 妹子
/* 赤裸裸的数学题 各种整体+插空 所以嘛 学好数学还是很重要的 ans=(n-1)!*(m-1)!*(2+(n-2)(n-1)) */ #include<iostream> #incl ...
- SQL 2008 清除数据库日志
USE [master]GOALTER DATABASE DNName SET RECOVERY SIMPLE WITH NO_WAITGOALTER DATABASE DNName SET RECO ...
- 秒味课堂Angular js笔记------过滤器
不同过滤器的小demo. currency number uppercase json limitTo date orderBy filter <script> var filterMy ...
- java使用内部类的好处及其初始化
java使用内部类的原因 每个内部类都能独立地继承自一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响 java内部类初始化 ForeCatal ...
- phpMyAdmin配置及 错误 缺少 mysqli 扩展。请检查 PHP 配置
PHPMyadmin配置文件config.inc.php内容如下,在需要设置的地方增加了相关注释. 非常适合对数据库操作命令不熟悉的数据库管理者,下面我就说下怎么安装该工具: 1.先到网上下载 ...
- 像table一样布局div的CSS属性详解
.equal { display:table; border-collapse:separate;margin: aut ...
- Linux samba服务器设置简单匿名共享
linux下面的samba非常的好用,很多人拿它来作共享文件服务器, 缺省配置下,samba必须提供用户名密码来访问,如果是所有人都可以访问的内容,那么是比较麻烦的,其实通过一个设置,即可实现不用输入 ...