【原创】leetCodeOj --- Jump Game II 解题报告
原题地址:
https://oj.leetcode.com/problems/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.)
方法:
这里比较微妙,尽管用dp硬解和用贪心的复杂度都是O(n^2),但贪心有常数上的优势,也的确ac了。用dp硬解则是TLE。
首先初始化终点fin为n - 1,然后从头开始,找到第一个能到达终点的点,并把终点更新为该点,同时步数加1。因为该点离终点只有一步之遥,所以经过该点到终点步数最短。
重复直到fin为0
全部代码:
class Solution {
public:
int jump(int A[], int n) {
if (n == )
return ;
int fin = n - ;
int step = ;
while (fin != )
{
for (int i = ;i < fin;i++)
{
if (A[i] >= fin - i)
{
fin = i;
step ++;
break;
}
}
}
return step;
}
};
【原创】leetCodeOj --- Jump Game II 解题报告的更多相关文章
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)
原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...
- 【LeetCode】45. Jump Game II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
随机推荐
- nginx+tomcat负载均衡策略
測试环境均为本地,測试软件为: nginx-1.6.0,apache-tomcat-7.0.42-1.apache-tomcat-7.0.42-2.apache-tomcat-7.0.42-3 利用n ...
- android app 架构设计02
二:在开放的过程中,尽量把工具类,BaseActivity 放在指定的位置. DateFormat Bitmap Notification Shared Preference Environment ...
- 使用和制作patch文件
使用和制作patch文件 发表时间: 2007-2-13 20:57 作者: superuser 来源: 迷茫人 字体: 小 中 大 | 打印 原文http://www.linuxsir. ...
- linux下动态连接变为静态打包,使用statifier_S展翅飞_新浪博客
linux下动态连接变为静态打包,使用statifier_S展翅飞_新浪博客 linux下动态连接变为静态打包,使用statifier (2013-04-27 14:38:19) 转载▼
- SQL_由创建表引出
***********************************************声明*************************************************** ...
- Linux 内核升级步骤
1.解压内核文件包#xz -d linux-3.2.63.tar.xz #tar xvf linux-3.2.63.tar 2.拷贝解压文件到/usr/src#cp -r linux-3.2.63 / ...
- 深入理解Tomcat系列之一:系统架构(转)
前言 Tomcat是Apache基金组织下的开源项目,性质是一个Web服务器.下面这种情况很普遍:在eclipse床架一个web项目并部署到Tomcat中,启动tomcat,在浏览器中输入一个类似ht ...
- filezilla安装
[alexus@wcmisdlin02 bin]$ ./filezilla ./filezilla: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not ...
- hdu 2082 生成函数
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2082 找单词 Time Limit: 1000/1000 MS (Java/Others) Me ...
- Study note for Continuous Probability Distributions
Basics of Probability Probability density function (pdf). Let X be a continuous random variable. The ...