[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 = ...
随机推荐
- 关于scanf的几种处理方法
字符输入中,赋值顺序和缓存的联系 scanf是从标准输入缓冲区中读取输入的数据,假设连续输入两个%c格式的字符.而中间又要涉及回车,那么第二个字符将被赋予回车. 解决的方法: .清空输入缓冲区 第一个 ...
- [Angular 2] Rendering an Observable Date with the Async and Date Pipes
Instead of simply pushing numbers on a timer into the template, now we'll move on to pushing actual ...
- 拿到手机ip住址
转载自:http://blog.csdn.net/showhilllee/article/details/8746114 貌似ASI里获取ip地址的链接不能够了.也曾试过whatismyip,在其站点 ...
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- ajax_post方式
test_ajax_post.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &quo ...
- POJ 1556 The Doors 线段判交+Dijkstra
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6734 Accepted: 2670 Descrip ...
- 查看SQL server服务名
net start MSSQL$SQLEXPRESS 启动服务命令 net stop MSSQL$SQLEXPRESS 关闭服务命令 网上看到的那些 我都用不了 最后想起了这个 现在好了
- oracle单行函数之类型转换
oracle数据类型转换:显示转换盒隐式转换 oracle自动完成转换
- 武汉科技大学ACM :1004: 零起点学算法74——Palindromes _easy version
Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.请写一个程序判断读入的字符串是否是“回文”. Input 输入包含多 ...
- [BZOJ]3737 [Pa2013]Euler
从这个FB开始写博客啦. 也不知道会坚持多久…… = =似乎要加一句转载请注明出处 http://www.cnblogs.com/DancingOnTheTree/p/4026076.html htt ...