跳跃游戏 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] 输出 ...
随机推荐
- 1048 Find Coins (25 分)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...
- Linux网络编程经典书籍推荐
UNIX环境高级编程<高级unix环境编程><unix网络编程><深入理解计算机系统>比较好 =====================Linux网络编程经典书籍推 ...
- PHP mysqli_free_result()与mysqli_fetch_array()函数
mysql_free_result() 仅需要在考虑到返回很大的结果集时会占用多少内存时调用.在脚本结束后所有关联的内存都会被自动释放. 在我们执行完SELECT语句后,释放游标内存是一个很好的习惯. ...
- Spark Streaming 例子
NetworkWordCount.scala /* * Licensed to the Apache Software Foundation (ASF) under one or more * con ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #20 使用fio进行I/O的基准测试
HACK #20 使用fio进行I/O的基准测试 本节介绍使用fio进行模拟各种情况的I/O基准测试的操作方法.I/O的基准测试中有无数需要考虑的因素.是I/O依次访问还是随机访问?是通过read/w ...
- 显式锁(三)读写锁ReadWriteLock
前言: 上一篇文章,已经很详细地介绍了 显式锁Lock 以及 其常用的实现方式- - ReetrantLock(重入锁),本文将介绍另一种显式锁 - - 读写锁ReadWriteLock. ...
- Android手机图片路径
H:\dcim\100MEDIA H:\Tencent\MobileQQ\photo H:\Tencent\MobileQQ\photo H:\Tencent\MobileQQ\thumb H:\Te ...
- c++官方文档-模版函数和重载
#include<stdio.h> #include<iostream> #include<queue> #include<map> #include& ...
- leetcode54
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) ...
- leetcode459
public class Solution { public bool RepeatedSubstringPattern(string s) { var len = s.Length; ) { ret ...