Jump Game - LeetCode
题目链接
注意点
解法
解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能。并且如果i超过了能到达的最大距离说明不能到达,因为i是每次加一都能超过最大距离,小于i的所有位置都会走到某个最远距离为0的位置。时间复杂度O(n)
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(),maxPos = 0;
for(int i = 0;i <= maxPos;i++)
{
if(maxPos >= n-1) return true;
maxPos = max(maxPos,i+nums[i]);
}
return false;
}
};
解法二:网上看来的解法 —— 动态规划。但是我并不能理解这个状态转移方程dp[i] = max(dp[i - 1], nums[i - 1]) - 1
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(),pre = nums[0];
for(int i = 1;i < n;i++)
{
pre = max(pre,nums[i-1])-1;
if(pre < 0) return false;
}
return true;
}
};
小结
- 希望有大神可以详说一下解法二的思路 TAT
Jump Game - LeetCode的更多相关文章
- 55. Jump Game leetcode
55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...
- Jump Game —— LeetCode
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Jump Game leetcode java
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- UNITY_委托和事件
UNITY_委托和事件 参考资料: Unity3D脚本编程-使用C#语言开发跨平台游戏-陈嘉栋 观察者模式 主题(Subject)管理某些数据,当主题的数据发生改变时,会通知已经注册(Register ...
- Python3的深拷贝和浅拷贝
a = 1 b = a a = 2 print(a, b) print(id(a), id(b)) """ 运行结果 2 1 1445293568 1445293536 ...
- 1.0 JAVA基础核心概念
JAVA基础知识 转载至:http://www.runoob.com/java/java-variable-types.html 对菜鸟教程进行核心整理: 一.JAVA理论概念 1.基础概念 Java ...
- 火狐插件安装-基于web自动化测试
一.Firebug 安装 1. 打开火狐浏览器—选择右上角“打开菜单”(图一)----附件组件(图二) 图一 图二 2. 点击:扩展(图三)—-------用于所有附加组件的工具(图四)----选 ...
- Streamr助你掌控自己的数据
博客说明 所有刊发内容均可转载但是需要注明出处. 项目简介 Streamr 致力于为世界实时数据的自由公平交换打造开源平台,并促进全球数据经济的发展.Streamr项目基于区块链技术,并向用户提供数据 ...
- Ubuntu系统无法识别Logitech M590蓝牙鼠标的问题
参见 - https://blog.csdn.net/yh2869/article/details/73119018 亲测可用. 系统:ubuntu 16.04 64bit 现象:鼠标配对可以成功,但 ...
- Sqlmap常用命令大全
1 Options(选项) -h,--help 显示帮助消息-hh 显示详细帮助-version -v VERBOSE 详细级别 0-6 默认12 Target 目标-u URL--url=URL-g ...
- 王者荣耀交流协会final发布-第一次scrum立会
1.例会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐 master:袁玥 2.时间跨度 2017年12月1日 17:00 — 17:31,总计31分钟 3.地点 一食堂二楼沙发座椅 ...
- jQuery控制a标签不可用
$('.disableCss').removeAttr('href');//去掉a标签中的href属性 $('.disableCss').removeAttr('onclick');//去掉a标签中的 ...
- 《Spring1之第九次站立会议》
<第九次站立会议> 昨天:对用C#写的视频功能进行了相关的了解. 今天:试着把用C#写的代码转换为java语言. 遇到的问题:说实话,真心不好转换,转换过程中遇到了很多问题.