算法(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 ...
随机推荐
- Mybatis中使用UpdateProvider注解实现根据主键批量更新
Mapper中这样写: @UpdateProvider(type = SjjcSqlProvider.class, method = "updateTaskStatusByCBh" ...
- ETO的公开赛T1《矿脉开采》题解(另类版)
这道题别看是签到题,写起来一点不简单 出题人的正解是双向搜索 我们把物品分成两半 每一半分别跑搜索 答案存下来,用个双指针合并即可 然后我构造了两组数据卡掉了他,不得不缩小数据范围 但我这里为什么要致 ...
- 关于java dom解析的问题
如下的xml代码: <persons> <person> <name>小强</name> <sex>male</sex> < ...
- Ubuntu16.04采用FastCGI方式部署Flask web框架
1 部署nginx 1.1 安装nginx服务 root@desktop:~# apt-get install nginx -y 1.2 验证nginx服务是否启动 root@des ...
- hive 学习系列五(hive 和elasticsearch 的交互,很详细哦,我又来吹liubi了)
hive 操作elasticsearch 一,从hive 表格向elasticsearch 导入数据 1,首先,创建elasticsearch 索引,索引如下 curl -XPUT '10.81.17 ...
- R语言绘图:雷达图
使用fmsb包绘制雷达图 library("fmsb") radarfig <- rbind(rep(90, 4), rep(60, 4), c(86.17, 73.96, ...
- POJ2739 Sum of Consecutive Prime Numbers 确定某个数以内的所有素数
参考:https://www.cnblogs.com/baozou/articles/4481191.html #include <iostream> #include <cstdi ...
- linux挂载命令mount及U盘、移动硬盘的挂载
一.mount的命令格式是(注意mount只能在root权限下运行) mount dervice dir dervice是要挂载的设备,dir是挂载点 二.查看当前磁盘列表的设备 fdisk -l 显 ...
- c++实验3类和对象
实 验 3: part 1:验证 part 2:graph #include <iostream> #include "graph.h" using namespac ...
- Android TV 开发(3)
本文来自网易云社区 作者:孙有军 <LinearLayout android:id="@+id/input_num_line_3" and ...