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.)
题目是假设输入数组能满足到达数组末尾的条件,求出最少的跳数。
题目给出的测试用例是[2,3,1,1,4]
第一次从开头跳,则跳得位置为0+A[0]=2,故在位置2
根据贪心策略下次跳的时候应该选择1~2之间跳的最远的位置开始跳
1的位置跳的距离为1+A[1]=4
2的位置跳的距离为2+A[2]=3
故下一步选择从1的位置开始跳刚好能跳到最后结束
class Solution {
public:
int jump(int A[], int n) {
int res = , last = , cur = ;
for(int i = ; i < n; ++ i){
if(i > last) last = cur,res++;
cur = max(cur,A[i]+i);
}
return res;
}
};
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 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 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
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 ...
- [Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【To Read】LeetCode | Jump Game II(转载)
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
随机推荐
- ecshop 支付
支付分成两部分 1.订单信息 2.支付日志ID 3.生成支付代码 一次性支付完成 // 支付信息 include_once('includes/lib_payment.php'); $order['l ...
- 如何合并两个Docker 镜像
http://www.open-open.com/lib/view/open1437746544709.html 在你的机器上使用docker pull来从Docker Hub下载镜像. docker ...
- 使用count结合nvl函数时碰到的问题
count()函数功能:统计表中中某个字段或所有记录个数,字段值为null的不做统计. 手册中解释: COUNT returns the number of rows returned by the ...
- 初次使用并安装express
安装 Nodejs 去Nodejs官网根据自己的操作系统下载对应的安装包并安装.我们就有了NodeJS和npm环境.npm是Node的包管理工具,会在安装NodeJS时一并安装.可以用以下命令查看版本 ...
- [Kerberos] Java client访问kerberos-secured cluster
使用java client访问kerberos-secured cluster,最重要的是先从admin那里拿到可用的keytab文件,用来作认证.接下来就是调整连接的配置.以下先用连接hdfs为例进 ...
- int类型的正负数转换
int aid = -this.id; 不能直接转 必须先赋值给一个变量 int c = this.id; int a = c * (-1); this.id = a;
- 【Go入门教程6】interface(interface类型、interface值、空interface{}、嵌入interface、反射)
interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...
- PHP5不重新编译,如何安装自带的未安装过的扩展,如soap扩展?
在虚拟机的CentOS5.5中,一键安装了PHP运行环境,但发现并没有 soap 扩展,而近期项目用需要用到 webservice. 上述的一键安装(lamp0.4),其实是源码编译安装,PHP配置文 ...
- Apache 启动.htaccess 的操作方法
配置apache主配置文件(httpd.conf) 1.查找“#LoadModule rewrite_module modules/mod_rewrite.so” 去掉前面的#号 2.把AllowOv ...
- Shodan新手入坑指南
*本文原创作者:xiaix,本文属FreeBuf原创奖励计划,未经许可禁止转载 亲们~黑五 Shodan Membership 只要5刀,你剁手了没? 什么是 Shodan? 首先,Shodan 是一 ...