经典算法——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.)
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; class Solution {
public:
int jump(vector<int> A)
{
int maxReach = A[0];
int edge = 0; //edge表示当前能够达到最远的坐标边界值
int minstep = 0; for (int i = 1; i < A.size(); i++)
{
//若当前坐标超过了最远的坐标边界值,应该跳跃一次,同一时候更新maxReach
if (i > edge)
{
minstep += 1;
edge = maxReach;
if (edge >= A.size() - 1) //若数组最后一个元素的坐标在edge覆盖的范围,则返回跳跃次数
return minstep;
}
maxReach = max(maxReach, A[i] + i);
}
//假设不能达到数组最后一个元素,则返回0
return 0;
}
}; int main()
{
Solution sol;
vector<int> nums = { 5,9,3,2,1,0,2,3,3,1,0,0 };
int res =sol.jump(nums);
cout << res << endl;
system("pause");
return 0;
}
经典算法——Jump Game(II)的更多相关文章
- 机器学习经典算法详解及Python实现--基于SMO的SVM分类器
原文:http://blog.csdn.net/suipingsp/article/details/41645779 支持向量机基本上是最好的有监督学习算法,因其英文名为support vector ...
- 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解题记录]Jump Game和Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- LeetCode 045 Jump Game II
题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...
- Java中的经典算法之冒泡排序(Bubble Sort)
Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...
- Atitit 图像处理30大经典算法attilax总结
Atitit 图像处理30大经典算法attilax总结 1. 识别模糊图片算法2 2. 相似度识别算法(ahash,phash,dhash)2 3. 分辨率太小图片2 4. 横条薯条广告2 5. 图像 ...
- Java中的经典算法之选择排序(SelectionSort)
Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...
- 57. Jump Game && Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
随机推荐
- [ Python - 12 ] 线程的信号量、标志位及队列
线程的信号量 线程的信号量是同时允许一定数量的线程更改数据,主要作用在于限制线程的并发. #!_*_coding:utf-8_*_ # Author: hkey import threading, t ...
- 识别浏览器的JavaScript引擎的方法
答案来自StackOverflow,打开这个网页http://jsbin.com/opuvas即可,这个网页也是答题者自己写的. 二维码是这个网址.网页内有统计访问量,作者想知道对多少人有用,建议尊重 ...
- POJ 1008 Maya Calendar / UVA 300【日期转换/常量数组】
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 82431 Accepted: 25319 Descr ...
- hdu6121
hdu6121 题意 给出一棵树,\(0\) 为根节点,节点 \(i\) 的父节点标号是 \(\lfloor\frac{i-1}{k}\rfloor\),求所有子树大小的异或和. 分析 找规律.在纸上 ...
- AOJ 2230 How to Create a Good Game(费用流)
[题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2230 [题目大意] 给出一张图,从1到n的最长路不变的情况下, 还能 ...
- [Contest20180323]King
跳蚤国王要召开第二届内阁会议,所以把所有跳蚤都召集到了会议室.所有跳蚤在会议室的圆桌前坐成了一个圈,从$1$到$n$标号,每人的面前都有一盏明灯. 就在会议就要开始的时候,国王突然发现,并不是所有的灯 ...
- 【分块】bzoj2957 楼房重建
http://www.cnblogs.com/wmrv587/p/3843681.html ORZ 分块大爷.思路很神奇也很清晰. 把 块内最值 和 块内有序 两种良好的性质结合起来,非常棒地解决了这 ...
- 【莫队算法】【权值分块】bzoj3585 mex
orz PoPoQQQ. 本来蒟蒻以为这种离散化以后就对应不起来的题不能权值分块搞的说. ……结果,实际上>n的权值不会对答案作出贡献. #include<cstdio> #incl ...
- 【AC自动机+DP】USACO2012 JAN GOLD_Video Game Combos
[题目大意] 给你个模式串(每个长度≤15,1≤N≤20),串中只含有三种字母.求一长度为K(1≤K≤1000)的字符串,使得匹配数最大(重复匹配计多次),输出最大值. [解题思路] W老师给的题,然 ...
- [HDU6252]Subway Chasing
题目大意: 一条直线上有n个点,两个人在直线上走,保持x的距离. 告诉你m条信息,告诉你一个人在ab之间时,另一个人在cd之间. 问这些信息是否矛盾,如果不矛盾,求相邻两点之间的最小距离. 思路: m ...