题目:

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. (Hard)

分析:

第一眼看完题目感觉用动态规划肯定能解,但是感觉就是有很多重复计算在里面...因为题目只问了个最少步数。

果然写出来之后华丽超时,但是代码还是记录一下吧

 class Solution {
public:
int jump(vector<int>& nums) {
int dp[nums.size()];
for (int i = ; i < nums.size(); ++i) {
dp[i] = 0x7FFFFFFF;
}
dp[] = ;
for (int i = ; i < nums.size(); ++i) {
for (int j = ; j < i; ++j) {
if (j + nums[j] >= i) {
dp[i] = min(dp[i], dp[j] + );
}
}
}
return dp[nums.size() - ];
}
};

优化考虑类似BFS的想法,维护一个步数的范围和一个能走到的最远距离。

算法就是遍历一遍数组,到每个位置的时候,更新他能到达的最远距离end,如果一旦超过nums.size() - 1,就返回step + 1;

curEnd维护以当前步数能到达的最远范围,所以当i > curEnd时,step++,并且将curEnd更新为end。

代码:

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

LeetCode45 Jump Game II的更多相关文章

  1. 57. Jump Game && Jump Game II

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

  2. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  3. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

  4. LeetCode: Jump Game II 解题报告

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

  5. 【LeetCode】45. Jump Game II

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

  6. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

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

  7. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

  8. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  9. [leetcode解题记录]Jump Game和Jump Game II

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

随机推荐

  1. js_sl 分享

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. cordova,phonegap 重力感应

    3.0版本后,cordova通过插件模式实现设备API,使用CLI的plugin命令可以添加或者移除插件: $ cordova plugin add org.apache.cordova.device ...

  3. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  4. 常见的mongo shell命令

    启动mongo shell 在windows下,双击mongo.exe可以启动mongo shell 查询库.表及选择库 查询所有库命令: show dbs 应用某一个db use jxs_datab ...

  5. linux which 查看可执行文件的位置

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:        which  查看可执行文件的位置.       whereis 查看文件的位置.         ...

  6. IOC知识

    1.两个基本概念 IOC(Inversion of Control ):反转控制,即将控制权反转出去. DI(Dependency Injection):依赖注入,根据依赖关系进行注入. DI是实现I ...

  7. HDU 3586 Information Disturbing (二分+树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  8. struts2 集成 easyui

    关键点: json数据格式 获取json数据 输出json 分页 #json数据格式# datagrid: {"total":1,"rows":[{" ...

  9. java -X 这不是标准的选项只是为了获取帮助信息

    -? -help      输出此帮助消息 获取帮助信息方式有三种: java java -? java -help -X            输出非标准选项的帮助 java -X -Xms< ...

  10. const型类成员

    一.关于const类成员函数有以下几个需要注意的地方: 1. 在普通的非const成员函数中,this的类型是一个指向类类型的const指针,而const成员函数中,this的类型是一个指向const ...