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 ...
随机推荐
- P4878 [USACO05DEC]layout布局
P4878 [USACO05DEC]layout布局 差分约束 最短路径最长路,最长路径最短路 本题求的是最长路径,所以跑最短路 根据题意连边,然后spfa即可 注意要判断图的连通性,所以新建一个虚拟 ...
- Linux服务器上Tomcat的Web工程部署
Linux服务器上Tomcat的Web工程部署 部署Web应用到Tomcat服务器就是将开放好的JavaWeb应用打包成war包,然后发布到tomcat服务器的webapps目录下: 步骤1,先进入t ...
- 编写第一个微信小程序界面
编写第一个微信小程序界面 不忘初心,方得始终:初心易得,始终难守. 传统的 web 结构 小程序文件目录结构 小程序页面层级结构 编写第一个小程序 1. 创建小程序目录结构 2. 编写代码 welco ...
- 20145106 《Java程序设计》第8周学习总结
教材学习内容总结 NIO即New IO.对于高级输入/输出处理.java从JDK1.4开始提供了NIO,在JAVA SE 7 中又提供了NIO2,认识这些高级输入/输出处理API(Applicatio ...
- Android项目开发二
微博客户端开发 本周学习计划 学习布局控件和UI设计相关知识. 微博验证,学习OAuth相关知识. 看懂微博客户端开发部分代码. 把借鉴代码导入到Android Studio中并运行成功. 实际完成情 ...
- fatal One or more refs for names blocks change upload
前言 今天在提代码时,发现push不到gerrit仓库了,十分的奇怪,和同事沟通后发现,同事可以直接git push origin master而且也可以合并,都是没有问题的,但是就是在gerrit上 ...
- Python3基础 try-指定except-as reason 捕获打开一个不存在的文件的时候,会产生OSError异常的示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 字符编码(ASCII、ANSI、GB2312、UTF-8等)系统梳理(转载)
引言 在显示器上看见的文字.图片等信息在电脑里面其实并不是我们看见的样子,即使你知道所有信息都存储在硬盘里,把它拆开也看不见里面有任何东西,只有些盘片.假设,你用显微镜把盘片放大,会看见盘片表面凹凸不 ...
- The equation (扩展欧几里得)题解
There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer r ...
- 论文笔记之:End-to-End Localization and Ranking for Relative Attributes
End-to-End Localization and Ranking for Relative Attributes arXiv Paper 摘要:本文提出一种 end-to-end 的属性识别方 ...