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 ...
随机推荐
- RabbitMQ入门:主题路由器(Topic Exchange)
上一篇博文中,我们使用direct exchange 代替了fanout exchange,这次我们来看下topic exchange. 一.Topic Exchange介绍 topic exchan ...
- 高级PHP工程师所应该具备的专业素养
初次接触PHP,就为他的美所折服,于是一发不可收拾. 很多面试,很多人员能力要求都有“PHP高级工程师的字眼”,如果您真心喜欢PHP,并且您刚起步,那么我简单说说一个PHP高级工程师所应该具备的,希望 ...
- [算法总结] 20 道题搞定 BAT 面试——二叉树
本文首发于我的个人博客:尾尾部落 0. 几个概念 完全二叉树:若二叉树的高度是h,除第h层之外,其他(1~h-1)层的节点数都达到了最大个数,并且第h层的节点都连续的集中在最左边.想到点什么没?实际上 ...
- vue mock(模拟后台数据) 最简单实例(一)——适合小白
开发是前后端分离,不需要等待后台开发.前端自己模拟数据,经本人测试成功. 我们在根目录新建存放数据的json文件,存放我们的数据data.json //data.json{ "status& ...
- 图解Raid5数据存储的原理
- Linux基础入门--06
简单的文本处理 实验介绍 这一节我们将介绍这几个命令:tr.col.join.paste 1.tr: -d:删除和set1匹配的字符,不是全词匹配也不是按字符顺序匹配 -s:除去指定的连续并重复的字符 ...
- 每日scrum(1)
今天又正式开始了第二个冲刺周期,计划十天,主要需要改进的地方包括UI界面,还有一些细节的把握. 今天出现的主要问题有:在讨论UI界面风格的时候,小组内部意见不统一,对UI界面的创作流程不熟悉,以及难度 ...
- JavaScript实现大整数减法
继上一篇博文写了大整数加法之后,我又模拟上篇博文的算法,自己实现了大整数减法. 大整数减法相对于加法来说,稍微复杂一点.由于要考虑一些情况: 1. 两个数相减,可能会出现结果为正.负和0三种情况: 2 ...
- ORACLE_SQL
--建立学生表create table Student ( Sno char(9) primary key, Sname char(20)unique, Sex ...
- DFS--障碍在指定时间会消失
哈利被困在了一个魔法花园里.魔法花园是一个 N*M 的矩形,在其中有着许多植物, 这些植物会在时刻 K 的倍数消失. 哈利每单位时间都会选择上.下.左.右四 个方向的其中一个进行移动. #includ ...