[LeetCode] 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.)
... | i-1 | i |
... | 3 | 2 |
这个可以这么思考,如果i 是从i-1 跳到的,那么i位置的最少跳数 <i> = <i-1> +1.
- 创建等长的字符串a,a[i] 表示到i-th 的最少跳数,有a[0]等于0,结果就是a[n-1]。
- 创建最大已知位置索引maxIdx,表示在i-th 位置时,已经最远确定的位置索引,初始化为1,即指向未知的位置最小的。
- 遍历数组。
- 如果遍历位置加上跳跃长度大于等于maxIdx,那么更新数组a 到最大索引maxIdx
- 如果maxIdx ==n 跳出
- 返回a[n-1].
这样遍历的时间其实只有n 次,时间O(n),空间也是O(n).改进地方就是空间改O(1)。
#include <iostream>
using namespace std; class Solution {
public:
int jump(int A[], int n) {
int *a = new int [n];
for(int i=;i<n;i++)
a[i]=INT_MAX;
a[]=;
int maxidx = ;
for(int i=;i<n;i++){
while(maxidx<=i+A[i]&&maxidx<n){
// for(int kk=0;kk<n;kk++) cout<<a[kk]<<" ";
// cout<<endl;
a[maxidx++] = a[i]+;
}
}
int ret = a[n-];
delete []a;
return ret;
}
}; int main()
{
int a[]={,,,,};
Solution sol;
cout<<sol.jump(a,sizeof(a)/sizeof(int))<<endl;
return ;
}
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] 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 ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II(贪婪算法)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode–jump game II
1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...
- leetcode Jump Game II python
@link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...
- 【To Read】LeetCode | Jump Game II(转载)
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
随机推荐
- formpanel布局的学习
FormPanel有两种布局:form和column,form是纵向布局,column为横向布局.默认为后者.使用layout属性定义布局类型.对于一个复杂的布局表单,最重要的是正确分割,分割结果直接 ...
- 绘制字符串:imagestring()
<?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...
- Liunx环境--Node部署记录
1.看看环境里有没有装Node which node 2.找个目录安装 (1)/usr/local/node/download 执行下载 wget https://nodejs.org/dist/v8 ...
- A1083 List Grades (25)(25 分)
A1083 List Grades (25)(25 分) Given a list of N student records with name, ID and grade. You are supp ...
- hadoop启动时权限不足
之前在使用时的没用去懂.ssh,后来因为一些情况直接将其权限修改为777. 第一位7等于4+2+1,所以就是rwx,所有者有读取.写入.执行的权限:第二位7也是4+2+1,rwx,同组用户具有读取.写 ...
- 4 Template层-CSRF
1.csrf 全称Cross Site Request Forgery,跨站请求伪造 某些恶意网站上包含链接.表单按钮或者JavaScript,它们会利用登录过的用户在浏览器中的认证信息试图在你的网站 ...
- B树、B-树、B+树、B*树之间的关系
https://blog.csdn.net/u013411246/article/details/81088914
- 学好java,做好工程师必读的15本书
学好java,做好工程师必读的15本书 一.Java编程入门类 对于没有Java编程经验的程序员要入门,随便读什么入门书籍都一样,这个阶段需要你快速的掌握Java基础语法和基本用法,宗旨就是“囫囵 ...
- 【Word Break】cpp
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- ios开发学习笔记003-流程控制和类型转换
流程控制 顺序结构.选择结构.循环结构 1.顺序结构 程序默认是顺序执行的. 2.选择结构 if选择语句 第一种情况 if(条件)//条件成立执行下面语句 { //语句 } 第二种情况 if(条件)/ ...