[leetcode]55.JumpGame动态规划题目:跳数游戏
/**
* 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.
动态规划题目,方法和找最大子串差不多,两个题目共同的特点是
1.每遍历一个元素都有可能影响最后的结果
2.都有局部解和最终解
3.都是通过解决局部解这个小问题而逐渐解决最终解的大问题
做这个题一开始的思路是:回溯法,从第一个数开始走,可能走的步数是[0,nums[i]],遍历可能走的步数,设置一个
变量记录这一步走到哪里了,下一次递归从变量处开始走,发现不能走了之后就返回,这样时间复杂度取决于元素的大小
但是肯定比O(n)大,提交发现超时了
后来发现正确解法是动态规划。局部解就是i + nums[i],全局解就是最大的局部解。每次遍历开始先判断能不能走到这一步
也就是(glo >= i)?,不符合的话直接break,因为如果能到达最后,肯定前边的都能到达。
最后比较glo和nums.length-1的大小。
注意遍历的最终点事nums.length-2,数组的最后一个元素是不遍历的。
*/
public class Q55JumpGame {
public static void main(String[] args) {
int[] nums = new int[]{2,3,1,1,4};
System.out.println(canJump(nums));
}
public static boolean canJump(int[] nums) {
//判断是不是能走到这里
if (nums.length == 1)
return true
int loc;
int glo = 0;
boolean res = false;
for (int i = 0; i < nums.length-1; i++) {
if (glo < i)
break;
//局部解和全局解
loc = i+nums[i];
glo = Math.max(glo,loc);
}
if (glo >= nums.length-1)
res = true;
return res;
}
}
[leetcode]55.JumpGame动态规划题目:跳数游戏的更多相关文章
- [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 ...
- [bzoj1978][BeiJing2010]取数游戏 game_动态规划_质因数分解
取数游戏 game bzoj-1978 BeiJing-2010 题目大意:给定一个$n$个数的$a$序列,要求取出$k$个数.假设目前取出的数是$a_j$,那么下次取出的$a_k$必须保证:$j&l ...
- leetcode 55 Jump Game 三种方法,回溯、动态规划、贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [SinGuLaRiTy] 动态规划题目复习
[SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...
- LeetCode初级算法--动态规划01:爬楼梯
LeetCode初级算法--动态规划01:爬楼梯 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况
Given an integer array A, and an integer target, return the number of tuples i, j, k such that i &l ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- 1166 矩阵取数游戏[区间dp+高精度]
1166 矩阵取数游戏 2007年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description [ ...
随机推荐
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- 深圳-2020-java面试题分享
记录一下最近面试接触的面试题. 深圳掌众传媒: union 和union all区别 union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: union All:对两个结果集进行 ...
- 如何使用TradingView(TV)回测数字货币交易策略
更多精彩内容,欢迎关注公众号:数量技术宅.想要获取本期分享的完整策略代码,请加技术宅微信:sljsz01 TradingView平台简介 前段时间,有粉丝找到技术宅,表示他有一个常用的交易平台,叫做T ...
- python批量definition query
import arcpy mxd = arcpy.mapping.MapDocument("current") for lyr in arcpy.mapping.ListLayer ...
- 禅道的bug提交
- 洛谷 P3410 拍照(最大流 + 建图)
这道题问的是一群人要和另一群人合影,每个客人都有必须在场的人全部在场才能在场,每个客人给的有收入,但是邀请也需要支出,问最大收入? 我觉得可以总结为一类问题,就是有先决条件的网络流问题.看到费用和支出 ...
- PyQt(Python+Qt)学习随笔:containers容器类部件QStackedWidget重要方法介绍
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 StackedWidget堆叠窗口部件为一系列窗口部件的堆叠,对应类为QStackedWidget. ...
- Python正则表达式书写容易碰到的陷阱:\W*和\W*?匹配过程遇到的问题
老猿在分析<Python正则表达式\W+和\W*匹配过程的深入分析>中的问题时,想到一个问题,如果"re.split('(\W*)','Hello,world')"的处 ...
- 第15.14节 PyQt(Python+Qt)入门学习:Designer的Buttons按钮详解
一.引言 Qt Designer中的Buttons部件包括Push Button(常规按钮.一般称按钮).Tool Button(工具按钮).Radio Button(单选按钮).Check Box( ...
- PyQt(Python+Qt)实战:使用QCamera、QtMultimedia等实现摄像头拍照
一.概述 在PyQt中,可以使用QCamera.QCameraViewfinder.QCameraViewfinderSettings等一系列多媒体操作相关类实现摄像头操作.用这些类不足50行代码+U ...