leetcode笔记:Jump Game
一. 题目描写叙述
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.
二. 题目分析
该题的大意是,给定一个数组,从第一个元素開始,元素的值表示可以往后跳的最大距离,问依照这样的规则,该数组能否跳到最后一个元素。
解题的基本思路是贪心算法。当然。使用动态规划也是全然可以解决的,也贴出网上一种动规代码。
三. 演示样例代码
// greedy algorithm
class Solution {
public:
bool canJump(int A[], int n) {
if(n == 0 || n == 1){
return true;
}
int maxStep = A[0];
for(int index = 1 ; index < n ; ++index)
{
if(maxStep == 0) return false;
--maxStep;
if(maxStep < A[index])
maxStep = A[index];
if(maxStep + index >= n - 1) // 满足条件
return true;
}
}
};
// DP algorithm
class Solution {
public:
bool canJump(int A[], int n)
{
vector<int> f(n, 0);
f[0] = 0;
for (int i = 1; i < n; i++)
{
f[i] = max(f[i - 1], A[i - 1]) - 1;
if (f[i] < 0) return false;
}
return f[n - 1] >= 0;
}
};
四. 小结
该题的思路是,使maxStep一直保持最大的能移动步数。
leetcode笔记:Jump Game的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
随机推荐
- SqlBulkCopy实现大批量数据导入
//自增列重新生成:SqlBulkCopy bc = new SqlBulkCopy(conn) //自增列保留原值:SqlBulkCopy bc = new SqlBulkCopy(conn,Sql ...
- treetable adding nodes at root level
describe("loadBranch()", function() { beforeEach(function() { this.newRows = " ...
- facade模式-服务-配置----系统生成与配置
facade模式对外提供一组相关服务: 对内整合子系统: facade模式的创建过程需要依赖外部的配置. 配置完成以后才能使用服务. 推广开来,所有系统都需要生成与配置,然后才能对外提供服务.
- JDBC配置MSSQL
使用JDBC连接SQL SERVER 这可能是个很老套的话题,但不管怎么说还是有用的.姑且把配置方法贴出来吧.1. 确认Sql Server的的运行状态打开Sql Server配置管理器,确认Sql ...
- docker 1-->docker compose 转载
转自:http://www.ityouknow.com/docker/2018/03/22/docker-compose.html Docker-Compose 是 Docker 的一种编排服务,是一 ...
- Java基础——面向对象(封装——继承——多态 )
对象 对象: 是类的实例(实现世界中 真 实存在的一切事物 可以称为对象) 类: 类是对象的抽象描述 步骤: 1.定义一个类 (用于 描述人:) ( * 人:有特征和行为) 2.根据类 创建对象 -- ...
- ionic3开发环境搭建与配置(win10系统)
1.安装nodeJS(不会的自行百度) 2.安装ionic和cordova,执行以下命令: npm install -g ionic cordova 3.安装Java JDK: 下载地址:http:/ ...
- 洛谷——P3807 【模板】卢卡斯定理
P3807 [模板]卢卡斯定理 洛谷智推模板题,qwq,还是太弱啦,组合数基础模板题还没做过... 给定n,m,p($1\le n,m,p\le 10^5$) 求 $C_{n+m}^{m}\ mod\ ...
- 深入分析同步工具类之AbstractQueuedSynchronizer
概览: AQS(简称)依赖内部维护的一个FIFO(先进先出)队列,可以很好的实现阻塞.同步:volatile修饰的属性state,哪个线程先改变这个状态值,那么这个线程就获得了优先权,可以做任何事 ...
- linux whereis-查找二进制程序、代码等相关文件路径
推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 whereis命令用来定位指令的二进制程序.源代码文件和man手册页等相关文件的路径. whereis命令只能用于程序名的搜索,而且 ...