【LeetCode】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.
java code : 这题初看用DP的思路很清晰: 设DP[i] := 从第 i 个位置开始能否到底最后一个位置,那么转移方程很好写
dp[A.length - 1] = true; 看代码吧,不过会超时,因为复杂度是O(n^2)
public boolean canJump(int[] A)
{
if(A.length <= 1)
return true;
if(A[0] >= (A.length - 1))
return true;
boolean[] dp = new boolean[A.length];
dp[A.length - 1] = true; for(int i = A.length - 2; i >= 0; i--)
{
if(i + A[i] >= A.length - 1)
{
dp[i] = true;
continue;
}
for(int j = i + 1; j < A.length - 1; j++)
{
if(i + A[i] >= j)
{
dp[i] |= dp[j];
if(dp[i] == true)
break;
}
}
}
boolean res = false;
if(dp[0] == true)
res = true;
dp = null;
return res;
}
下面给出一种O(n)的算法:
我们用maxlength 维护一个从开始位置能到达的最远距离,然后判断在当前位置是否能够到底最后一个位置和当前位置是否可达,如果两个条件都满足,那么返回true,如果当前位置是0,并且最远距离不能超过当前位置,那么只能返回false 了,更新最远距离
java code : 416ms
public class Solution {
public boolean canJump(int[] A) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(A.length <= 1)
return true;
if(A[0] >= (A.length - 1))
return true;
int maxlength = A[0];
if(maxlength == 0)
return false;
for(int i = 1; i < A.length - 1; i++)
{
if(maxlength >= i && (i + A[i]) >= A.length - 1)
return true;
if(maxlength <= i && A[i] == 0)
return false;
if((i + A[i]) > maxlength)
maxlength = i + A[i];
}
return false;
}
}
顺便说一下我看讨论区有人贴出如此算法并且还过了:
class Solution {
public:
bool canJump(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int i = 0;
while( i < n-1)
{
if( A[i] == 0)
return false;
i += A[i];
}
return i >= n-1;
}
};
这个算法是错误的,因为有一组反列: A = {5,4,0,0,0,0,0}, 显然应该返回 false.
leetcode 的数据还是有点弱。
【LeetCode】Jump Game (一维动态规划 + 线性扫描)的更多相关文章
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- LeetCode总结 -- 一维动态规划篇
这篇文章的主题是动态规划, 主要介绍LeetCode中一维动态规划的题目, 列表如下: Climbing StairsDecode WaysUnique Binary Search TreesMaxi ...
- 【hihocoder 1249 Xiongnu's Land】线性扫描
2015区域赛北京赛区的三水,当时在赛场上没做出的原因是复杂度分析不正确导致把方法想复杂了.近来复习复杂度分析,觉得不能只是笼统地看渐进复杂度(big-O),更应根据算法的伪码计算真正的以基本操作数为 ...
- [ An Ac a Day ^_^ ] HihoCoder 1249 Xiongnu's Land 线性扫描
拿到了icpc北京站的参赛名额 感谢亮哥~ 虽然是地狱之战 但也要全力以赴! 题意: 有一片沙漠 n片绿洲 让你用一条线分成两部分 左≥右 而且分割线要尽量靠右 问线的位置 思路: 网上说可以二分 没 ...
- 【CF56E】Domino Principle(线性扫描,伪DP)
每块多米诺骨牌所在的位置设为x,每块多米诺骨牌高度为h.如果将x位置上的多米诺骨牌向右翻到,它就可以影响[x+1, x+h-1]范围内的所有多米诺骨牌,让他们也翻到,同时这些被翻到的多米诺骨牌还能影响 ...
- 【CF676C】Vasya and String(二分查找,线性扫描尺取法)
题意: 给出一个长度为n的字符串,只有字符'a'和'b'.最多能改变k个字符,即把'a'变成'b'或把'b'变成'a'. 问改变后的最长连续相同字符的字串长度为多少. 首先是二分查找,好想也好写 .. ...
- LeetCode探索初级算法 - 动态规划
LeetCode探索初级算法 - 动态规划 今天在LeetCode上做了几个简单的动态规划的题目,也算是对动态规划有个基本的了解了.现在对动态规划这个算法做一个简单的总结. 什么是动态规划 动态规划英 ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode刷题-- 5. 动态规划
动态规划思路 参考 状态转移方程: 明确「状态」-> 定义dp数组/函数的含义 -> 明确「选择」-> 明确 base case 试题 53最大子序和 题目描述 53 给定一个整数数 ...
随机推荐
- ftp缓存信息
using System.Collections.Generic; using NewTempo.Ftp; using System.IO; using NshowAdClient.NshowAdSe ...
- 不通过注册表使用ActiveX对象
为了弄清楚COM库的运行原理,特意做了这个实验: #include "stdafx.h" #include "objbase.h" #include " ...
- Python并发编程-线程锁
互斥锁-Lock #多线程中虽然有GIL,但是还是有可能产生数据不安全,故还需加锁 from threading import Lock, Thread #互斥锁 import time def ea ...
- JavaScript初步
隐式转换 其他类型转换成布尔类型: undefined --> false null --> false 0或者0.0或者NaN --> false 字符串长度为0 --> f ...
- BZOJ1975 SDOI2010魔法猪学院
就是个A*,具体原理可以参考VANE的博文. 正解要手写堆,会被卡常,也许哪天我筋搭错了写一回吧. #include<bits/stdc++.h> #define r register u ...
- 我是如何从一个xss到某个浏览器的远程命令执行
0x01 前言:其实我是个小白平时就喜欢瞎搞,无意间碰到一个浏览器就想一探究竟,好了废话不多说开始!!! 0x02 可以看到我打开的新标签是怎么一个链接页面,既然是页面我是不可以XSS它呢? 于是我就 ...
- 20172333 2017-2018-2 《Java程序设计》第11周学习总结
20172333 2017-2018-2 <Java程序设计>第11周学习总结 教材学习内容 对于Android Studio的安装以及对安卓的一些基本组成,比如说四大组件Acticity ...
- CountDownLatch源码分析
CountDownLatch.Semaphore(信号量)和ReentrantReadWriteLock.ReadLock(读锁)都采用AbstractOwnableSynchronizer共享排队的 ...
- Linux知识(5)----LINUX下GDB调试
命令 解释 示例 file 加载被调试的可执行程序文件.因为一般都在被调试程序所在目录下执行GDB,因而文本名不需要带路径. (gdb) file gdb-sample r c Run的简 ...
- 使用Win2D在UWP程序中2D绘图(一)
在新的Windows UWP程序中,引入了一个新的API库: Win2D.它是一个d2d的封装,可以直接使用C#来快速实现高效2D绘图了.这个API虽然在Win8.1时代就开始着手开发了,但最近才完善 ...