55. 45. Jump Game II *HARD*
1.
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.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4]
, return true
.
A = [3,2,1,0,4]
, return false
.
bool canJump(vector<int>& nums) {
int n = nums.size();
if(n <= )
return true;
int coverPos = , maxPos = -, t, next, i, j, k = ;
for(i = ; i < n && i <= coverPos; i++)
{
t = nums[i] + i;
if(t > coverPos)
coverPos = t;
if(coverPos >= n-)
return true;
}
return false;
}
2.
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.)
int jump(vector<int>& nums) {
int n = nums.size();
if(n <= )
return ;
int coverPos = , maxPos = -, t, next, i, j, k = ;
for(i = ; i < n && i <= coverPos;)
{
t = i + nums[i];
if(t > coverPos)
{
coverPos = t;
k++;
}
if(coverPos >= n-)
break;
for(j = i+; j <= coverPos; j++)
{
t = j + nums[j];
if(t > maxPos)
{
maxPos = t;
next = j;
}
}
i = next;
}
return k;
}
55. 45. Jump Game II *HARD*的更多相关文章
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- 【LeetCode】45. 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#55, 45]Jump Game, Jump Game II
The problem: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- ImageLoader作用 AAAA
https://github.com/nostra13/Android-Universal-Image-Loader ImageLoader作用 1.多线程下载图片,图片可以来源于网络,文件系统,项目 ...
- 解决Linux下jdk版本与安装版本不一致
解决Linux下jdk版本与安装版本不一致 在Linux下安装jdk后,利用java -version查看版本使,发现不是自己所安装的jdk版本; 解决方案: which java ——查看默认的jd ...
- mongodb 最佳可视化工具mongobooster
最好用的mongodb GUI工具 mongobooster,没有之一,可从https://mongobooster.com/下载 常见管理命令可参考,http://www.cnblogs.com/l ...
- nginx location正则写法
nginx location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # ...
- map set iterator not incrementable 解决办法
例子: #include <iostream> #include <map> using namespace std; int main() { map<int, int ...
- 螺旋折线|2018年蓝桥杯B组题解析第七题-fishers
标题:螺旋折线 如图p1.png所示的螺旋折线经过平面上所有整点恰好一次. 对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度. 例如dis(0, ...
- 日志统计|2018年蓝桥杯B组题解析第八题-fishers
标题:日志统计 小明维护着一个程序员论坛.现在他收集了一份"点赞"日志,日志共有N行.其中每一行的格式是: ts id 表示在ts时刻编号id的帖子收到一个"赞" ...
- VS2010下创建WEBSERVICE,第二天 ----你会在C#的类库中添加web service引用吗?
本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...
- java代码实现highchart与数据库数据结合完整案例分析(一)---饼状图
作者原创:转载请注明出处 在做项目的过程中,经常会用到统计数据,同时会用到highchart或echart进行数据展示,highchart是外国开发的数据统计图插件, echart是我们国家开发的数据 ...
- HDU 2460 Network(桥+LCA)
http://acm.hdu.edu.cn/showproblem.php?pid=2460 题意:给出图,求每次增加一条边后图中桥的数量. 思路: 先用tarjan算法找出图中所有的桥,如果lowv ...