经典算法——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 ...
随机推荐
- celery-分布式任务队列-原理
# 转自:https://www.cnblogs.com/forward-wang/p/5970806.html 在学习Celery之前,我先简单的去了解了一下什么是生产者消费者模式. 生产者消费者模 ...
- 定义序列化器时的read_only和write_only选项
# 转载请留言联系 read_only read_only表示只能读,不能进行修改.例如定义序列化器时,id字段通常指定read_only=True.在序列化时,即对象转为字典.JSON字符串时,字典 ...
- python tips:列表推导
看一个代码: a=[1,2,3,4,5,6,7,8,9] b=[5 if (i >3) else 1 for i in a] print(b) 这就是列表推导. 列表推导一般用在通过一个list ...
- 利用beamer做幻灯片插入EPS图片的方法
可以利用epstopdf宏包,在命令行下利用pdflatex带"-shell-escape"参数编译. 先插入epstopdf宏包:\usepackage{graphicx}\ ...
- C# 日期格式化{0:yyyy-MM-dd HH:mm:ss.fff}和{0:yyyy-MM-dd hh:mm:ss.fff}的区别(转)
日期格式化{0:yyyy-MM-dd HH:mm:ss.fff}和{0:yyyy-MM-dd hh:mm:ss.fff}的区别http://www.cnblogs.com/jhxk/articles/ ...
- [centos] 需要 libmpc.so.2 提供下载
http://pan.baidu.com/s/1kTmmthH yum update 的时候需要libmpc.so.2, 于是下载了一个 rpm -ivh filename.rpm 安装上就好了
- [jquery]判断checkbox是否选中
if ($("#id").is(":checked"))
- ZCMU Problem H: Crixalis's Equipment(贪心,排序)
#include<stdio.h> #include<stdlib.h> struct node { int a,b; }c[1002]; int cmpxy(const st ...
- 百度之星资格赛 2016 Problem 1002
本文链接:http://www.cnblogs.com/Ash-ly/p/5494623.html 题意: 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个 ...
- POJ 1776 Task Sequences(竞赛图构造哈密顿通路)
链接:http://poj.org/problem?id=1776 本文链接:http://www.cnblogs.com/Ash-ly/p/5458635.html 题意: 有一个机器要完成一个作业 ...