Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目。题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置。
比如:
A = [2, 3, 1, 1, 4], return true.
一种走法是 0 - 2 - 3 - 4,还有一种走法是 0 - 1 - 4
O(n ^ 2) 解法
一个很显然,几乎不用动脑的解法。
设置一个布尔数组f,f[0] === true 表示 index === 0 这个位置能够到达,模拟每个位置的前进,最后判断 f[lastIndex] 的值。
/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function(nums) {
var f = [];
f[0] = true;
nums.forEach(function(item, index, array) {
if (f[index]) {
var tmp = Math.min(array.length - 1, index + item)
for (var i = index + 1; i <= tmp; i++)
f[i] = true;
}
});
return f[nums.length - 1] ? false : true;
};
但是返回了无情的TLE,给出了一组TLE的数据,数组长度达到了25000,也就是复杂度达到了O(25000 ^ 2),虽然leetcode数据普遍很弱,但是这组TLE也是让我心服口服。
解法1,跪...
O(nlogn) 解法
方法总比困难多,联想到了树状数组中的染色问题。
我们可以把jump的过程看成是染色,还是从左到右枚举位置,比如枚举到 index=0 位置时,nums[0]=5,也就是说从 index=0 的位置一直可以走到 index=5 的位置,那么我们可以把1~5这一段进行染色。当枚举到 index=1 时,如何判断能不能走到这一步呢?只需求该点被染色的次数,如果大于0,那么就是能到达,然后从该点向后继续染色,最后判断最后一点有没有被染色即可。复杂度 O(nlongn)。
/**
* @param {number[]} nums
* @return {boolean}
*/
var sum, n;
function lowbit(x) {
return x & (-x);
}
function update(index, val) {
while (index) {
sum[index] += val;
index -= lowbit(index);
}
}
function getAns(index) {
var ans = 0;
while (index <= n) {
ans += sum[index];
index += lowbit(index);
}
return ans;
}
var canJump = function(nums) {
sum = [];
sum[1] = 1;
n = nums.reduce(function(pre, item, index) {
return Math.max(pre, item + index + 1);
}, 0);
for (var i = 2; i <= n; i++)
sum[i] = 0;
for (var i = 0, len = nums.length; i < len; i++) {
var isPainted = getAns(i + 1); // 是否被染色
if (!isPainted) continue;
update(i + 1 + nums[i], 1);
update(i, -1);
}
return Boolean(getAns(len));
};
O(n) 解法
用树状数组显然大材小用了,树状数组可以求得被染色的次数,但是本题只需要判断是否被染色即可;而且本题每次染色都是一次。
进一步思考,我们枚举每一位时都在判断是否被染色过(从而决定是否能够到达该点且能否继续往前走),假设在某一瞬间,index=m 的位置已经被染色了,那么 index=n (n<=m) 的位置肯定已经被染色过了,我们维护一个最右边被染色的点,如果当前枚举点在该点的左侧,那么当前点已经被染色,否则即可停止遍历(因为右边的点再也不可能被染色到了)。
/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function(nums) {
var rightMost = 1;
for (var i = 0, len = nums.length; i < len; i++) {
if (rightMost < i + 1) break;
rightMost = Math.max(rightMost, i + 1 + nums[i]);
}
return rightMost >= len;
};
Jump Game 的三种思路 - leetcode 55. Jump Game的更多相关文章
- Maximal Rectangle [leetcode] 的三种思路
第一种方法是利用DP.时间复杂度是 O(m * m * n) dp(i,j):矩阵中同一行以(i,j)结尾的所有为1的最长子串长度 代码例如以下: int maximalRectangle(vecto ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- 简谈百度坐标反转至WGS84的三种思路
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 基于百度地图进行数据展示是目前项目中常见场景,但是因为百度地图 ...
- Java中实现十进制数转换为二进制的三种思路
Java中实现十进制数转换为二进制 第一种:除基倒取余法 这是最符合我们平时的数学逻辑思维的,即输入一个十进制数n,每次用n除以2,把余数记下来,再用商去除以2...依次循环,直到商为0结束,把余数倒 ...
- 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 三种方法,回溯、动态规划、贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- mysql5.6主从参数详解
mysql5.6的主从相当的不错,增加了不少参数,提升了主从同步的安全和效率,以下是mysql5.6主从参数详解. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- vc6.0快捷键大全
转载! 按下Alt 键不放,点击鼠标左键拖动,可以选择文本块.可选择列: ...
- zookeeper 相关学习资料
zookeeper的配置:http://www.cnblogs.com/yuyijq/p/3438829.html zookeeper运维:http://blog.csdn.net/hengyunab ...
- 聚合数据董铭彦:小程序开发的兴起将带火API数据交易
2016中关村大数据日活动近日在京举办,今年新进驻北京的聚合数据受邀参与,在13日举行的大数据交易专场论坛上,聚合数据副总裁董铭彦与参会嘉宾以"共筑数据交易产业生态,共享大数据时代红利&qu ...
- 新版Microsoft Azure Web管理控制台 - Microsoft Azure New Portal - (1)
国际版Microsoft Azure的Preview Portal已经于12月2日正式GA,现在登录国际版的Microsoft Azure,默认就会进入Microsoft Azure New Port ...
- Java实现点击一个控件实现删除一个控件的方法
最近在做项目的时候需要处理点击一个JLabel实现删除这一个JLabel的功能.最近折磨了一点时间,查了一下API.找到2个方法可以实现这个功能. remove public void remove( ...
- 用PS设计等高线效果的背景图片
有些简单的单网页,如果利用等高线效果的背景图片,再配合合适的背景色,能达到绚丽的效果.如下图所示: 本文就介绍该等高线效果的背景图片是如何制作的.Follow Me!!!! 1.新建文档,尺寸:100 ...
- Miller-Rabin素数快速检测
满足费马小定理 a^(n-1) === 1(mod n) --->伪素数 对于所有a belong Zn*,总存在满足的合数n,称为Carmichael数 ------------- ...
- AC日记——欧几里得的游戏 洛谷 P1290
题目描述 欧几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数M和N,从Stan开始,从其中较大的一个数,减去较小的数的正整数倍,当然,得到的数 ...
- Gvr SDK for Unity 分析(一)
Gvr SDK概述 通过谷歌VR SDK for unity 为Android和iOS 构建虚拟现实应用程序 unity SDK在Android上支持构建应用程序for daydream 和 card ...