LeetCode——Jump Game II
Description:
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.)
贪心
评论区有一个答案Orz:https://leetcode.com/discuss/422/is-there-better-solution-for-jump-game-ii
/*
* 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 = 0;
int last = 0;
int curr = 0;
for (int i = 0; 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 跳跃游戏之二
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 ...
- [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 ...
随机推荐
- iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件
iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件 在一个项目中用了Easy UI,但是发现里面的 Dialog .Window.Messager 弹窗都不支 ...
- 配置editplus,讓其支持代碼自動格式化功能.
使用editplus已經好多年了,累積了不少的東西,想換IDE比較麻煩,所以就研究了一下用editplus搭配gofmt.exe配置go語言代碼自動格式化的功能.還好功夫不負有心人,終於被我搞懂了,不 ...
- spring官方案例程序
https://github.com/spring-projects/spring-data-book https://github.com/spring-projects 包含其他相关的应用程序
- CMD command
过滤字符串查找:netstat -aon|findstr "80"
- 集群: 如何在spring 任务中 获得集群中的一个web 容器的端口号?
系统是两台机器, 跑四个 web 容器, 每台机器两个容器 . nginx+memcached+quartz集群,web容器为 tomcat . web 应用中 用到spring 跑多个任务,任务只能 ...
- MySQL 学习用employee数据库表参考使用
download place:https://launchpad.net/test-db/ ,choose this file from the right panel:employees_db-fu ...
- how to read from __consumer_offsets topic
来自:http://grokbase.com/t/kafka/users/15bs2r0m83/kafka-0-8-2-1-how-to-read-from-consumer-offsets-topi ...
- 第50讲:Scala中Variance变化点
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- 记录一下,如何配置nodejs nginx的反向代理
本文是在mac下配置nodejs 在nginx下的反向代理 1.安装nodejs,之前就安装了. 2.安装nginx ,我采用的直接源码安装 3.进入 /usr/local/nginx/conf 目录 ...
- 对list集合中的对象按照对象的某一属性进行排序
/** * 重新对list中的CmsCyUser对象按照最终的票数进行排序 * @param list */ private void reSort(List list) { Object[ ...