Jump Game 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], return true.

A = [3,2,1,0,4], return false.

说是贪心算法,其实就是记录数组中每个元素可以移动的最大位置。之前思路不太清晰,写代码一定要有清晰的思路啊,而且想问题应该要从多个角度去想才对。

class Solution {
public:
bool canJump(vector<int>& nums) {
int size=nums.size();
// int flag=0;
if(size==)
return ;
int maxstep=nums[];
for(int i=;i<size;i++)
{
if(maxstep==)
return ;
maxstep--;
maxstep=max(maxstep,nums[i]); }
return ; }
};

Jump Game 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 is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

Note:
You can assume that you can always reach the last index.

仍然使用贪心,只不过是要记录当前一跳所能到达的最远距离、上一跳所能达到的最远距离,和当前所使用跳数就可以了代码如下:

class Solution {
public:
int jump(vector<int>& nums) {
int len=nums.size();
int cur=;
int last=;
int res=;
for(int i=;i<len;i++)
{
if(i>cur)
return -;
if(i>last)
{
last=cur;
res++;
}
cur=max(cur,i+nums[i]);
}
return res;
}
};
  

Jump Game I&&II——入门级贪心算法的更多相关文章

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

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

  2. [LeetCode] 122. 买卖股票的最佳时机ii best-time-to-buy-and-sell-stock-ii(贪心算法)

    思路: 只要第二天的价格高于第一天,就进行交易.(这样的话就默认可以同一天内先卖出再买进) class Solution(object): def maxProfit(self, prices): & ...

  3. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  4. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  5. [算法导论]贪心算法(greedy algorithm)

    转载请注明出处:http://www.cnblogs.com/StartoverX/p/4611544.html 贪心算法在每一步都做出当时看起来最佳的选择.也就是说,它总是做出局部最优的选择,寄希望 ...

  6. 【贪心算法】POJ-2376 区间问题

    一.题目 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cle ...

  7. POJ 2287 田忌赛马 贪心算法

    田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...

  8. 「面试高频」二叉搜索树&双指针&贪心 算法题指北

    本文将覆盖 「字符串处理」 + 「动态规划」 方面的面试算法题,文中我将给出: 面试中的题目 解题的思路 特定问题的技巧和注意事项 考察的知识点及其概念 详细的代码和解析 开始之前,我们先看下会有哪些 ...

  9. 《Java算法》贪心算法

    贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 贪心算法的经典案例: 跳跃游戏: 给定一个非负整 ...

随机推荐

  1. [转载]php中sleep,flush,ob_flush函数介绍

    <?phpecho str_pad(" ",1024);//当上面这句没有的时候浏览器没有任何输出 直到sleep函数设定的时间结束 才会输出//原因如下面截图for ($i ...

  2. distcc配置

    原理图: OS: ubuntu Server 12.04 1.安装 apt-get install distcc 2.配置 将文件/etc/default/distcc修改为如下格式 STARTDIS ...

  3. Difference between List View and DataGrid in WPF

    Well, in WPF the difference between ListView and DataGrid is just one. Editing. You need editing use ...

  4. Eclipse srever起来时,时间超过45s。

    双击servere的名字,在属性界面上进行修改. 如下图: 修改TimeOut中的值即可.

  5. Tomcat设置开启时自动访问某个servlet类存在的问题

    <servlet> <servlet-name>****</servlet-name> <servlet-class>****</servlet- ...

  6. JAVA--未明确定义列

    原因:列明重复 可以加前缀或区别名以区分

  7. 深度解析Java多线程的内存模型

    内部java内存模型 硬件层面的内存模型 Java内存模型和硬件内存模型的联系 共享对象的可见性 资源竞速 Java内存模型很好的说明了JVM是如何在内存里工作的,JVM可以理解为java执行的一个操 ...

  8. zk-web

    Ref:https://github.com/qiuxiafei/zk-web zk-web是一个用clojure with noir and boostrap写的Zookeeper WEB UI管理 ...

  9. 【洛谷 P4008】 [NOI2003]文本编辑器 (Splay)

    题目链接 \(Splay\)先练到这吧(好像还有道毒瘤的维护数列诶,算了吧) 记录下光标的编号,维护就是\(Splay\)基操了. 另外数据有坑,数据是\(Windows\)下生成了,回车是'\n\r ...

  10. Machine Learning(CF940F+带修改莫队)

    题目链接:http://codeforces.com/problemset/problem/940/F 题目: 题意:求次数的mex,mex的含义为某个集合(如{1,2,4,5})第一个为出现的非负数 ...