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. 【逆序对相关/数学】【P1966】【NOIP2013D1T2】 火柴排队

    传送门 Description 涵涵有两盒火柴,每盒装有 $n$ 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为:$ \sum ...

  2. 直通BAT面试算法精讲课 --动态规划

    1.有数组penny,penny中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim(小于等于1000)代表要找的钱数,求换钱有多少种方法. 给定数 ...

  3. Block中的循环引用警告

  4. 学习tcpIp必备的抓包工具wireshark

    wireshark是一个优秀的抓包工具 ip.src=192.168.10.123  发送http的一端 ip.dst=192.168.10.126 接收http的一端 如下图所示:

  5. JN5169 --------- zigbee代码

    队列: 需要创建3个标准队列(只创建不使用): ------Queue with handle zps_msgMlmeDcfmInd to receive IEEE 802.15.4 MACcomma ...

  6. tp if condition in_array用法

    <if condition="in_array($vo['status'],[3])"> <a href="javascript:void(0);&qu ...

  7. 在浏览器输入网址,Enter之后发生的事情

    简介: 1. 浏览器接收域名 2. 发送域名给DNS,中文名字是域名系统服务器,一般位于ISP(互联网服务提供商,比如我们熟知的联通.移动.电信等) 中.浏览器会首先发给离自己最近的DNS,DNS收到 ...

  8. .net core 安装Swagger

    Install-Package Swashbuckle -Pre 1.Startup // This method gets called by the runtime. Use this metho ...

  9. Java面试知识总结三

    1.SpringMVC在项目中的应用? 1)service层,项目的业务逻辑层,一般先定义一个接口,在写一个实现类,实现所有的接口方法.service的实现类中要加注解@Service(用于标注业务层 ...

  10. [uva11997]k个最小和

    一个k*k的矩阵,每行选取一个数相加则得到一个和,求最小的前k个和. k<=750 已知前m行最小的前k个和d[1]…d[k],则前m+1行最小的前k个和都必定是d[i](i<=k)+a[ ...