算法(5)Jump Game
题目:非负数的数组,每个数组元素代表这你能最大跨越多少步,初始在0的位置,问,能不能正好调到数组的最后一位!
https://leetcode.com/problems/jump-game/#/description
思路1:从尾部记录每个元素能不能到达末尾,算法复杂度O(n*n)【当时想出这个算法,还自以为不错,但是leetcode说算法超时呀,被分分钟教做人】
思路2:对这整个数组中的数据结构深刻的理解!(参考:http://blog.csdn.net/linhuanmars/article/details/21354751),首先明白一个问题:只要数组中不含有0,那么这个数组肯定是可以跳过去的!==> 得到一个结论是:只要能跳到数组外面,那么我们就一定能够跳到最后一个位置!所以就看下从数组中第一个位置开始,最远能跳到哪里就好了!之前的疑惑包括:1)怎么确定从第一个下标开始能跑多远呢?难道我们不需要递归这一路上所有的步数可能吗?万一路上遇到一个0,那不直接盼死刑,我们就得退一步,然后求少走的这条路啊!这就是之前的症结所在,是一种线性思维!于是有了参考网页中的算法:我们同时记录全局最优和局部最优。全局最优,实时更新,全局最优之间的每一个节点,目前都是可以到达的知道吗!呃呃呃,需要分析的东西好多,并且这些东西都是解题思路的关键呢!那么只要在这个范围之内的节点能够到达更远的地方,那么全局的最远的点就应该被更新!
答案:
https://github.com/honpey/codebox/blob/master/leetcode/array/p55.cpp
算法(5)Jump Game的更多相关文章
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- JPS/JPS+ 寻路算法
目录 概念 强迫邻居(Forced Neighbour) 跳点(Jump Point) JPS 寻路算法(Jump Point Search) 实现原理 示例过程 JPS+(Jump Point Se ...
- Jump Game I&&II——入门级贪心算法
Jump Game I Given an array of non-negative integers, you are initially positioned at the first index ...
- jump game(贪心算法)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 经典算法——Jump Game(II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- [原创]基于rsync算法的目的性改进-RexSync
rsync是一种文件差异传输的算法,特点是高效且相似块识别率较高.具体算法这边就不赘述,网上很多,官方文档也描述的很清楚. rsync提高文件比对效率的一个核心算法之一就是rolling checks ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- linux命令系列-ln(软硬链接)
linux命令 ln命令可以生成软链接和硬链接,也可叫做符号链接和实体链接. 有兴趣深入理解的可以查阅相关文档,一般的读者只需记住以下几点即可: .不管是软链接还是硬链接都不会额外增加磁盘空间(虽然实 ...
- linux wdcp3 上传大文件 服务器i/o错误
在一次上传大文件时 提示 服务器i/o错误 找了些方法都没有解决 最后发现 wdcp3 面板 默认安装时 web服务器引擎是 nginx + apache 共用 而且 nginx 默认并没与安装 ...
- IDEA项目启动报Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed"
启动报错: Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed&q ...
- CPS---(Cyber-Physical Sytem,信息物理融合系统)
1.CPS定义 CPS是连接计算机虚拟世界与物理现实世界的系统.---We refer to systems that bridge the cyber-world of computing and ...
- unity独立游戏开发日志2018/09/22
f::很头痛之前rm做的游戏在新电脑工程打不开了...只能另起炉灶... 还不知道新游戏叫什么名...暂且叫方块世界.(素材已经授权) 首先是规划下场景和素材文件夹的建立. unity常用的文件夹有: ...
- sort()的部分用法
#include <iostream> #include <cstdio> #include <algorithm>//sort要包含的头文件 #include & ...
- python基础之进程间通信、进程池、协程
进程间通信 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 进程队列queue 不同于线程queue,进程 ...
- [KAFKA]kafka常用操作
-- kafka路径示例 /opt/cloudera/parcels/KAFKA/bin-- kafka启动./kafka-server-start.sh -daemon ../config/serv ...
- CDH,CM下载
wget -c -r -nd -np -k -L -A rpm http://archive-primary.cloudera.com/cdh5/parcels/latest/ http://arch ...
- 用intellij Idea加载eclipse的maven项目全流程
eclipse的maven项目目录 全流程 加载项目 打开intellij Idea file -> new -> module from existing Sources 选择.pom ...