算法(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

时间: 2024-10-02 03:19:06

算法(5)Jump Game的相关文章

[LeetCode] 时间复杂度 O(n),空间复杂度 O(1) 的动态规划算法,题 Jump Game

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 ex

leetcode_55题——Jump Game(贪心算法)

Jump Game Total Accepted: 43051 Total Submissions: 158263My Submissions Question Solution 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 le

经典算法——Jump Game(II)

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. Your goal is to reach the last index in the minimum number of jumps

经典算法——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 =

LeetCode45 Jump Game II

题目: 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. Your goal is to reach the last index in the minimum number of j

HDU 4862 Jump (最小K路径覆盖)

HDU 4862 Jump 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4862 题意:给定一个N*M的矩阵,矩阵里面为0~9的数字.现在规定从一个点可以跳到它正下方和正右方的点,花费的费用为曼哈顿距离 - 1.如果在跳的过程中,两个点的数字相同,那么将得到该点的数字.规定可以从任意点开始跳,每个点只能经过1次.最多可以选择K个点来作为起点进行跳跃.问能否经过所有的点,如果可以,那么花费的费用是多少. 思路: 如果是最小路径覆盖,那么很容易构造图.但

HDU 4004 The Frog's Games(基本算法-贪心,搜索-二分)

The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The

LeetCode 045 Jump Game II

题目要求:Jump Game II 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. Your goal is to reach the last index in the minim

算法模板——AC自动机

实现功能——输入N,M,提供一个共计N个单词的词典,然后在最后输入的M个字符串中进行多串匹配(关于AC自动机算法,此处不再赘述,详见:Aho-Corasick 多模式匹配算法.AC自动机详解.考虑到有时候字典会相当稀疏,所以引入了chi和bro指针进行优化——其原理比较类似于邻接表,这个东西本身和next数组本质上是一致的,只是chi和bro用于遍历某一节点下的子节点,next用于查询某节点下是否有需要的子节点) 1 type 2 point=^node; 3 node=record 4 ex: