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. Node.js + Express 构建的订餐系统

    Node.js的版本 - v0.8.12 Express的版本 – v3.3.3  (安装 $ npm install -g express) 系统的登录逻辑是:获取用户名 + 密码,向内网RTX服务 ...

  2. NameError: name 'reduce' is not defined

    听说了大名鼎鼎的 reduce 函数,可以是执行的时候却显示这个. In [1]: reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) ---------------- ...

  3. Java工程师成神之路~(2018修订版)

    针对本文,博主最近在写<成神之路系列文章> ,分章分节介绍所有知识点.欢迎关注. 主要版本 更新时间 备注 v1.0 2015-08-01 首次发布 v1.1 2018-03-12 增加新 ...

  4. Excel 2016 Power View选项卡不显示的问题

    https://zhuanlan.zhihu.com/p/43543442 PowerView是Excel中的Power系列插件之一,可以基于excel制作交互式仪表板. 初学者在使用Power Vi ...

  5. 使用jstl标签时提示The absolute uri: http://java.sun.com/jsp/jstl/core cannot

    http://www.360doc.com/content/11/1219/15/1007797_173395882.shtml 检查应用目录下WEB-INF的lib里是否有jstl.jar和stan ...

  6. BERT的开源实现的使用

    参考这篇文章: 小数据福音!BERT在极小数据下带来显著提升的开源实现 https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&mid=224749 ...

  7. 怎么选择软件许可证,Apache, MIT, BSD, GPL, Mozilla, LGPL

  8. Libnids读书笔记 (转)

    一.当日工作(或学习)内容及进展情况(以条目式陈述,必要时配图说明) Libnids读书笔记: Libnids(Library Network Intusion Detection System)网络 ...

  9. 【Java】Java-UTC-时间戳处理

    Java-UTC-时间戳处理 java utc 时间戳_百度搜索 JAVA获取时间戳,哪个更快 - 潇湘客 - ITeye博客 Java获取UTC时间的方法详解_java_脚本之家 Java UTC时 ...

  10. sklearn文本特征提取

    http://cloga.info/2014/01/19/sklearn_text_feature_extraction/ 文本特征提取 词袋(Bag of Words)表征 文本分析是机器学习算法的 ...