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 ...
随机推荐
- ios 布局 素材 待整理
https://www.cnblogs.com/fxwl/p/5961372.html div区域 8.盒子模型的相关属性 margin(外边距/边界) border(边框) padding(内边距/ ...
- CAD使用DeleteXData删除数据(网页版)
主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 js代码实现如下 ...
- BZOJ 2693: jzptab 莫比乌斯反演 + 积性函数 +筛法
Code: #include<bits/stdc++.h> #define ll long long #define M 10001000 #define maxn 10200100 #d ...
- UVA - 10723 Cyborg Genes (LCS)
题目: 思路: 求两个串的最长公共子序列,则这个最短的串就是给出的两个串的长度和减去最长公共子序列的长度. 状态转移方程: 如果s[i-1]==t[j-1]就有dp[i][j] = dp[i-1][j ...
- ORM优化
orm优化数据库访问:https://docs.djangoproject.com/en/1.11/topics/db/optimization/ 一.QuerySet 可迭代 querysey=mo ...
- Django-REST-Framework JWT 实现SSO认证(下)
在上一篇博客中,我已经对JSON Web 认证做了简单的解释,这篇博客是续篇,若不了解,请看上一篇博客:https://www.cnblogs.com/yushenglin/p/10863184.ht ...
- Djang学习笔记-1
1.django的生命周期: url匹配 -> 视图函数 -> 返回用户字符串 url匹配 -> 视图函数 -> 打开一个HTML文件,并读取内容2.创建Django proj ...
- 06 Python流程控制
目录: 12) if语句 13) 三目运算 14) while语句 15) break与continue关键字 16) while…else语句 12,if语句 Note: 在一个if语 ...
- excel 2003 默认保存后出现超级连接解决方法
在excel 2003 中当选中某个单元格然后拷贝出来后发现总是出现超级连接,每次都要取消下很是麻烦 . 于是经过研究找到解决方法,真是累的我够呛 ,先将方法介绍给大家. 工具---自动更正选项--- ...
- validate针对checkbox、radio、select标签的验证
jQuery.validate 是jquery的一个插件,用来辅助开发者在客户端方便快捷的实现表单验证,最终达到提高用户体验的目的. 示例代码 <form id="formLogin& ...