跳跃游戏 12 · Jump Game 12
跳跃游戏 1
[抄题]:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 由于要用iteration, 不能return, 每次都要对can数组进行操作
- 循环函数中不用return false, 因为最终能不能是can数组决定的
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O() Space complexity: O()
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
跳跃游戏 2
[抄题]:
给出一个非负整数数组,你最初定位在数组的第一个位置。
数组中的每个元素代表你在那个位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
[思维问题]:
以为要比大小:确实是要比大小
[一句话思路]:
用steps[i] = steps[j] + 1来记录步数
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- steps[j]是从0开始往i靠近的 用j + A[j] >= i 表示角标能赶上
- steps[i]中只要有一个能赶上就足以break
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
不理解原理
[总结]:
通过角标能不能赶上,最终完成对steps[i]的迭代
[复杂度]:Time complexity: O(n^2) Space complexity: O(n^2)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
DP greedy一般不好随意出题,Google出题比较多。
[其他解法]:
greedy
[Follow Up]:
[LC给出的题目变变变]:
757. Set Intersection Size At Least Two 至少有两个交集:greedy
[代码风格] :
public class Solution {
public int jump(int[] A) {
// state
int[] steps = new int[A.length];
// initialize
steps[0] = 0;
for (int i = 1; i < A.length; i++) {
steps[i] = Integer.MAX_VALUE;
}
// function
for (int i = 1; i < A.length; i++) {
for (int j = 0; j < i; j++) {
if (steps[j] != Integer.MAX_VALUE && j + A[j] >= i) {
steps[i] = Math.min(steps[i], steps[j] + 1);
}
}
}
// answer
return steps[A.length - 1];
}
}
跳跃游戏 12 · Jump Game 12的更多相关文章
- [Swift]LeetCode45. 跳跃游戏 II | Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 55. 跳跃游戏(Jump Game)
题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II
55. 跳跃游戏 问题描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
- lintcode : 跳跃游戏
跳跃游戏 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 判断你是否能到达数组的最后一个位置. 样例 A = [2,3,1,1,4],返回 ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ
跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...
随机推荐
- python的return self的用法
转载:https://blog.csdn.net/jclian91/article/details/81238782 class foo: def __init__(self): self.m = 0 ...
- python pip使用报错:Fatal error in launcher: Unable to create process using '"'
在一个系统中共存Python2.python3的时候,pip.pip2.pip3使用的时候会报错: c:\Python35\Scripts>pip3Fatal error in launcher ...
- 0_Simple__simpleCooperativeGroups
▶ 协作组,CUDA9.0 的新特性 ▶ 源代码,如何获得协作组的编号? #include <stdio.h> #include "cuda_runtime.h" #i ...
- hive整合hbase
Hive整合HBase后的好处: 通过Hive把数据加载到HBase中,数据源可以是文件也可以是Hive中的表. 通过整合,让HBase支持JOIN.GROUP等SQL查询语法. 通过整合,不仅可完成 ...
- jpa-spring -basic
applicationContent.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- HTML5 Canvas ( 图片填充样式 ) fillStyle, createPattern
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- border做三角符号
用border做三角符号以及其他图形 ;; border-width:20px 10px; border-style:solid; border-color:#ff3300 #ff3300 #ffff ...
- VBA 使用QueryTables 中文乱码的处理
一般情况: cnn = "OLEDB;Provider=IBMDA400;Data Source=TFB4001;User ID=;Password=;" Sql = " ...
- xcode显示行号show gutter
要在每一个代码编辑窗口中的边线里显示行号: 使用Xcode > Preferences 菜单命令,点击 Text Editing,然后选择Editing 然后点击选择 “Line numbers ...
- iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 规范与部署
沪江CCtalk视频地址:https://www.cctalk.com/v/15114923889450 规范与部署 懒人推动社会进步. 本篇中,我们会讲述三个知识点 定制书写规范 开发环境运行 如何 ...