LeetCode: JumpGame 1 and 2
Title :
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
.
思路:
使用贪心算法,用maxStep来记录当前位置跳的最远距离,更新maxStep = max(A[i],maxStep),每前进一步,maxStep--
class Solution {
public:
bool canJump(vector<int>& nums) {
if (nums.size() < )
return false;
if (nums.size() == || nums.size() == )
return true;
int maxStep = nums[];
for (int i = ; i < nums.size(); i++){
if (maxStep == )
return false;
maxStep--;
maxStep = max(maxStep,nums[i]);
if (i + maxStep >= nums.size()-)
return true;
}
}
};
Jump Game2
Title
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.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2
. (Jump 1
step from index 0 to 1, then 3
steps to the last index.)
思路1 : 使用动态规划来做,不过超时
int jump(vector<int>& nums) {
int n = nums.size();
vector<int> result(n,INT_MAX);
result[] = ;
for (int i = ; i < nums.size(); i++){
for (int j = i+; j <= i+ nums[i]; j++){
if (j >= nums.size())
break;
result[j] = min(result[j],result[i]+);
}
}
return result[n-];
}
思路2 : 大牛写的扫描一遍。我仔细想了想,扫描一遍和动态规划有些相似之处。在动态规划中,我们需要对每个i更新下在他的jump范围内的其他点的跳数。那么扫面一遍的思路呢,是用两个变量last,cur来记录,last是记录之前的step下能跳的最远距离,cur则是记录下当前能到达的最远距离。更新last是在当前的i超过了last,则说明已经突破之前的势力范围,需要更新,用
http://www.cnblogs.com/lichen782/p/leetcode_Jump_Game_II.html 中的例子来说明
比如就是我们题目中的[2,3,1,1,4]。初始状态是这样的:cur表示最远能覆盖到的地方,用红色表示。last表示已经覆盖的地方,用箭头表示。它们都指在第一个元素上。
接下来,第一元素告诉cur,最远咱可以走2步。于是:
下一循环中,i指向1(图中的元素3),发现,哦,i小于last能到的范围,于是更新last(相当于说,进入了新的势力范围),步数ret加1.同时要更新cur。因为最远距离发现了。
接下来,i继续前进,发现i在当前的势力范围内,无需更新last和步数ret。更新cur。
i继续前进,接下来发现超过当前势力范围,更新last和步数。cur已然最大了。
最后,i到最后一个元素。依然在势力范围内,遍历完成,返回ret。
/*
* We use "last" to keep track of the maximum distance that has been reached
* by using the minimum steps "ret", whereas "curr" is the maximum distance
* that can be reached by using "ret+1" steps. Thus,
* curr = max(i+A[i]) where 0 <= i <= last.
*/
class Solution {
public:
int jump(int A[], int n) {
int ret = ;
int last = ;
int curr = ;
for (int i = ; i < n; ++i) {
if (i > last) {
last = curr;
++ret;
}
curr = max(curr, i+A[i]);
} return ret;
}
};
LeetCode: JumpGame 1 and 2的更多相关文章
- leetcode — jump-game
/** * Source : https://oj.leetcode.com/problems/jump-game/ * * Created by lverpeng on 2017/7/17. * * ...
- Leetcode::JumpGame
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [Leetcode 55]跳格子JumpGame
[题目] Given an array of non-negative integers, you are initially positioned at the first index of the ...
- [leetcode]55.JumpGame动态规划题目:跳数游戏
/** * Given an array of non-negative integers, you are initially positioned at the first index of th ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- [LeetCode]题解(python):055-Jump Game
题目来源 https://leetcode.com/problems/jump-game/ Given an array of non-negative integers, you are initi ...
随机推荐
- 【块状树】BZOJ 1086: [SCOI2005]王室联邦
1086: [SCOI2005]王室联邦 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 826 Solved: ...
- jQuery插件手把手教会(二)
上次我们将到了简单的jQuery插件,这次我们继续: 面向对象的插件开发 为什么要有面向对象的思维,因为如果不这样,你可能需要一个方法的时候就去定义一个function,当需要另外一个方法的时候,再去 ...
- 【转】System.DateTime.Now.ToString()的一些用法
C#中的日期处理函数 //2007年4月24日 this.TextBox6.Text = System.DateTime.Now.ToString("D"); ...
- HDU 1087 Super Jumping! Jumping! Jumping!(最长上升子序列,dp)
以下引用自:http://www.cnblogs.com/Lyush/archive/2011/08/31/2161314.html沐阳 该题可以算是一道经典的DP题了,题中数据是这样的.以 3 1 ...
- [C++]默认构造函数
默认构造函数(default constructor)就是在没有显示提供初始化式时调用的构造函数.它由不带参数的构造函数,或者为所有的形参提供默认实参的构造函数定义.若个定义某个类的变量时没有提供初始 ...
- POJ 1054 The Troublesome Frog(枚举+剪枝)
题目链接 题意 :给你r*c的一块稻田,每个点都种有水稻,青蛙们晚上会从水稻地里穿过并踩倒,确保青蛙的每次跳跃的长度相同,且路线是直线,给出n个青蛙的脚印点问存在大于等于3的最大青蛙走的连续的脚印个数 ...
- hibernate的简单学习(第一天)
sql脚本: -- Create table drop table T_PERSON; create table T_PERSON ( id ) PRIMARY KEY, name ), passwo ...
- JavaWeb项目开发案例精粹-第2章投票系统-006view层
1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- sudo和su
su命令就是切换用户的工具 sudo 授权许可使用的su,也是受限制的su 1. sudo 的适用条件 由于su 对切换到超级权限用户root后,权限的无限制性,所以su并不能担任多个管理员所管理的系 ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...