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. 如何设置Eclipse使用JDK

      1.打开Eclipse,选择Windows->Preferences,如图所示: 2.配置本地安装的jdk,如图所示: 注意:首先要先安装JDK.     木头大哥所发的文章均基于自身实践, ...

  2. rm删除破折号 - 开头的文件

    解决这个问题的一个方法就是在要删除的文件的前边加上"./" # rm ./-slow_query_130103.txt.gz To remove a file whose name ...

  3. SDUT3926 kmp

    bLue的二叉树 Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Problem Description Keke 是一个喜爱种树的 ...

  4. FreeRTOS 问题归纳

    分配给FreeRTOS的heap过少,可能出现任务创建不成功:也可能调用vTaskStartScheduler()后有返回(也就是执行下一条程序),实质上也是vTaskStartScheduler() ...

  5. javascript实现正整数分数约分

    //m,n为正整数的分子和分母 function reductionTo(m, n) { var arr = []; if (!isInteger(m) || !isInteger(n)) { con ...

  6. 数据结构:Treap

    关于重量平衡树的相关概念可以参考姊妹文章:重量平衡树之替罪羊树 Treap是依靠旋转来维护平衡的重量平衡树中最为好写的一中,因为它的旋转不是LL就是RR 对于每一个新的节点,它给这个节点分配了一个随机 ...

  7. SVN服务器更换IP,客户端重新定位

    svn服务器更换ip,后客户端需要重新定位,操作如下: 1.找到你的项目文件所在的根目录,右键点击空白地方,弹出菜单 TortoiseSVN-->Relocate 点击Relocate ,弹出重 ...

  8. 使用Docker搭建Django,Nginx,R,Python部署环境

    转载自https://blog.csdn.net/The_One_is_all/article/details/76063968 基本环境: Ubuntu 16.10 docker 17.06.0-c ...

  9. idea出现:error:java: Target level '1.7' is incompatible with source level '1.8'.解决办法

    当我们开始使用idea的时候,编译jsp程序我们有可能出现编译错误,然而我们的代码又没有什么问题. 解决方法一:我们开始的时候可以通过修改java compiler来解决这样的问题,点击file菜单- ...

  10. 省队集训 Day4 a

    [题目大意] 求有多少区间只包含1个出现次数为1的数. $1\leq n \leq 5*10^5, 0 \leq a_i \leq 10^9$ [题解] 考虑枚举右端点,设这个数上一次出现位置为pre ...