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 ...
随机推荐
- OpenCV Using Python——基于SURF特征提取和金字塔LK光流法的单目视觉三维重建 (光流、场景流)
https://blog.csdn.net/shadow_guo/article/details/44312691 基于SURF特征提取和金字塔LK光流法的单目视觉三维重建 1. 单目视觉三维重建问题 ...
- 记一次ping: unknown host错误
虚拟机上一台主机,之前一直在用,可以通过xshell连接,但是忽然发现ping百度失败了! [root@mgt02 ~]# ping www.baidu.com ping: unknown host ...
- PHP-ThinkPHP5砍价活动相关设计
近期我们公司项目里陆陆续续有很多为了招引新用户的活动推出,砍价的活动由我来负责,我们的项目是在微信浏览器里供用户浏览访问. 大概描述:进入砍价活动列表页选择有意向的商品,用户点击商品图片可以看到WEB ...
- ACM数论之旅6---数论倒数,又称逆元(我整个人都倒了( ̄﹏ ̄))
数论倒数,又称逆元(因为我说习惯逆元了,下面我都说逆元) 数论中的倒数是有特别的意义滴 你以为a的倒数在数论中还是1/a吗 (・∀・)哼哼~天真 先来引入求余概念 (a + b) % p = (a% ...
- Windows中的时间(SYSTEMTIME和FILETIME) (转载)
转载:http://blog.csdn.net/bokee/article/details/5330791 两种时间系统之间没有本质区别(事实上CRT时间是用Windows时间实现的,当然这是说的VC ...
- MacBook PRO蓝牙无法搜索设备
背景 经常把MacBook合上盖子就塞进包里,用时打开盖子就继续操作,偶尔会出现刚刚还在用的罗技蓝牙鼠标,重新打开笔记本后就连接不上了,而且也无法搜索到周边的蓝牙设备. 解决方案 快捷键:Option ...
- [BZOJ 2200][Usaco2011 Jan]道路和航线 spfa+SLF优化
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- [luogu 2458][SDOI2006]保安站岗
题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...
- Python非递归遍历多叉树
class Queue: def __init__(self,max_size): self.max_size = int(max_size) self.queue = [] def put(self ...
- 51nod 1444 破坏道路(最短路)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1444 题意: 思路: 哇,思路爆炸. 因为每条边的权值都为1,所以可以直 ...