LeetCode45 Jump Game II
题目:
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.)
Note:
You can assume that you can always reach the last index. (Hard)
分析:
第一眼看完题目感觉用动态规划肯定能解,但是感觉就是有很多重复计算在里面...因为题目只问了个最少步数。
果然写出来之后华丽超时,但是代码还是记录一下吧
class Solution {
public:
int jump(vector<int>& nums) {
int dp[nums.size()];
for (int i = ; i < nums.size(); ++i) {
dp[i] = 0x7FFFFFFF;
}
dp[] = ;
for (int i = ; i < nums.size(); ++i) {
for (int j = ; j < i; ++j) {
if (j + nums[j] >= i) {
dp[i] = min(dp[i], dp[j] + );
}
}
}
return dp[nums.size() - ];
}
};
优化考虑类似BFS的想法,维护一个步数的范围和一个能走到的最远距离。
算法就是遍历一遍数组,到每个位置的时候,更新他能到达的最远距离end,如果一旦超过nums.size() - 1,就返回step + 1;
curEnd维护以当前步数能到达的最远范围,所以当i > curEnd时,step++,并且将curEnd更新为end。
代码:
class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size() == ) {
return ;
}
int step = , end = , curEnd = ;
for (int i = ; i < nums.size(); ++i) {
if (i > curEnd) {
step++;
curEnd = end;
}
end = max(end, nums[i] + i);
if (end >= nums.size() - ) {
return step + ;
}
}
return -;
}
};
LeetCode45 Jump Game II的更多相关文章
- 57. Jump Game && Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【LeetCode】45. Jump Game II
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [leetcode解题记录]Jump Game和Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
随机推荐
- 【转】webgame前台开发总结--虽然是10年的文章,但是也有参考价值
一.webgame整个游戏流程: 1.预加载(打开游戏页面后,显示进度条,主要加载前期的登陆和创建角色资源,创建角色资源的加载可以放到进入创建角色界面的时候加载,因为玩家除了第一次进入游戏,其他时间基 ...
- U盘分区信息清除
diskpart select disk 1 clean 清除选中(优U)盘的所有信息;
- DataTrigger的几个用法
1.用在textbox等输入控件上,验证输入是否合法. <Window.Resources> <Style TargetType="TextBox"> &l ...
- 新 esb-cs-tool.jar 参数说明
旧esb-cs-tool.jar 使用说明 : invoke(RequestBusinessObject requestBo) 旧参数说明: requestBo : 封装好的请求参数大对象 Req ...
- JSF 2 button and commandButton example
In JSF 2.0, both <h:button /> and <h:commandButton /> tags are used to render HTML input ...
- POJ1384Piggy-Bank(DP)
POJ1384http://poj.org/problem?id=1384 最简单的完全背包问题,注意下初始化为INF就可以. #include <map> #include <se ...
- jsp转发action的问题找不到acton
-----------------------------jsp转发action的问题找不到acton------------------------------------------- jsp: ...
- Linux下安装Android Studio (Centos 7)
首先去下载一个android studio的包. http://www.android-studio.org/ (友情一个) http://www.cnblogs.com/gssl/p/4963668 ...
- Oracle用户、权限、角色管理
Oracle 权限设置一.权限分类:系统权限:系统规定用户使用数据库的权限.(系统权限是对用户而言). 实体权限:某种权限用户对其它用户的表或视图的存取权限.(是针对表或视图而言的). 二.系统权 ...
- 组合View Controller时遇到的一点问题
View Controller的组合应用其实很常见了,比如说Tab bar controller和Navigation view controller的组合使用,像这种一般都是Navigation v ...