I:

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], returntrue.

A =[3,2,1,0,4], returnfalse.

维护一个当前能跳到的最大值maxJump, 若是maxJump 已经>=nums.length-1, 说明能跳到最后一个点,return true.若是过程中maxJump <= i, 说明跳到当前点便不能往前,跳出loop, return false.

class Solution {
public:
bool canJump(int A[], int n) {
int cur=;
for(int i=;i<n;++i)
{
if(i>cur ||cur>=n-)
break;
cur=max(cur,i+A[i]);
}
return cur>=n-;
}
};

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 is2. (Jump1step from index 0 to 1, then3steps to the last index.)

class Solution {
public:
int jump(int A[], int n) {
vector<int> dp(n,);// dp存放都到各点的最小步数 for(int i=;i<n;++i)
{
int max=min(i+A[i],n-);// 从i点出发能走的最远距离
for(int j=i+;j<=max;++j)
{
if(dp[j]==)
dp[j]=dp[i]+;// 如果位置没被走过,则到达j点的步数为dp[i]+1
}
if(dp[n-]!=) break;// 当第一次到达终点时,肯定是到达终点最短的步数
}
return dp[n-];
}
};

jump-game i&&ii 能否跳出区间 贪心的更多相关文章

  1. HDU 1936 区间贪心

    /* *区间贪心.前几天刚做了POJ 1328 ...思路完全相同... *最多有100个表情,100行文字.遍历寻找每个表情的所在区间.时间复杂度大约在10^5 ~ 10^6 可以接受. *然后对每 ...

  2. TZOJ 4007 The Siruseri Sports Stadium(区间贪心)

    描述 The bustling town of Siruseri has just one sports stadium. There are a number of schools, college ...

  3. HDU 2037 今年暑假不AC (区间贪心)

    题意:又是中文题... 析:先说一下区间贪心的一个定理,选择不相交的区间:数轴上有n个开区间(ai, bi).选择尽量多的区间,使得这些区间两两不相交,贪心策略是,一定是选bi小的.(想一下为什么). ...

  4. UVA-11134 Fabled Rooks 贪心问题(区间贪心)

    题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...

  5. 【题解】P1712 [NOI2016]区间(贪心+线段树)

    [题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...

  6. 贪心思想之区间贪心 关联洛谷P1803

    力扣上也有一道类似的题 几乎是一样 输出不同 → 力扣leetcode 435. 无重叠区间 区间贪心是比较经典的 就拿洛谷P1803来举例 题目大意 n个比赛 [开始时间,结束时间] 问一个人最多能 ...

  7. 力扣leetcode 435. 无重叠区间 - 贪心

    非常经典的区间贪心思想 -- 详见博文: 贪心思想之区间贪心 本题给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] ...

  8. Jump Game I&&II——入门级贪心算法

    Jump Game I Given an array of non-negative integers, you are initially positioned at the first index ...

  9. leetcode Jump Game I II 待续 贪心看不懂啊!!!!

    下面是这两个题的解法: 参考博客:http://blog.csdn.net/loverooney/article/details/38455475 自己写的第一题(TLE): #include< ...

随机推荐

  1. 暴君第一季/全集Tyrant迅雷下载

    本季第一季 Tyrant Season 1 (2014)看点:虽然李安退出了FX系列剧<暴君>(Tyrant),称不想耽误了剧集的制作,但显然FX对这部剧的重视程度非比寻常,因为他们找来的 ...

  2. Android中的输入法

    提起输入法我就想到了Edittext,输入法可以自动根据inputType来改变键盘的布局,在支付钱包中还特别隐藏的系统自带的输入法,直接让用户用软件自己的输入法,提高了安全性.所以,我们应该对输入法 ...

  3. 《成神之路-基础篇》JVM——Java内存模型(已完结)

    Java内存模型 本文是<成神之路系列文章>的第一篇,主要是关于JVM的一些介绍. 持续更新中 Java内存模型 JVM内存结构 VS Java内存模型 VS Java对象模型(Holli ...

  4. 访问修饰符(C# 编程指南)

    所有类型和类型成员都具有可访问性级别,该级别可以控制是否可以从你的程序集或其他程序集中的其他代码中使用它们. 可以使用以下访问修饰符在进行声明时指定类型或成员的可访问性: public同一程序集中的任 ...

  5. iOS測试——置换測试: Mock, Stub 和其它

    文章地址:http://ryantang.me/blog/2014/08/21/test-doubles/

  6. C++中JSON的使用详解【转】

    https://blog.csdn.net/admin_maxin/article/details/53175779 jsoncpp 主要包含三个class:Value.Reader.Writer.注 ...

  7. Letter Combinations of a Phone Number leetcode java

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  8. 【Eclipse】Eclipse-Build-缓慢-卡住

    Eclipse-Build-缓慢-卡住 eclipse building workspace 卡主_百度搜索 解决building workplace 导致的卡死,使得eclipse加速 - CSDN ...

  9. wifidog 源码初分析(2)-转

    上一篇分析了接入设备的首次浏览器访问请求如何通过 防火墙过滤规则 重定向到 wifidog 的 HTTP 服务中,本篇主要分析了 wifidog 在接收到 接入设备的 HTTP 访问请求后,如何将此 ...

  10. eclipse help说明链接

    http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Freference%2Fcdt_u_prop_bu ...