题目:

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. 3.VS2010C++相关文件说明

    stdafx.h说明:stdafx的英文全称为:Standard Application Framework Extensions(标准应用程序框架的扩展).所谓头文件预编译,就是把一个工程(Proj ...

  2. [原创]Devexpress XtraReports 系列 2 创建表格报表

    昨天发表了Devexpress XtraReports系列开篇,今天我们继续. 今天的主题是创建表格报表. 首先我们来看看最后实现的效果.Demo最后附上. 接下来开始讲解如何一步一步做出这个报表: ...

  3. 应用TcpListener实现的socket服务器端

    前言 项目中要实现一个简单的socket服务器端,采用了TcpListener这个类.除了基本的功能之外,有几处需要注意的点. 要能同时接收多个客户端的连接,当然,不需要几千个那么多. 要能探测到客户 ...

  4. 对pymysql的简单封装

    #coding=utf-8 #!/usr/bin/python import pymysql class MYSQL: """ 对pymysql的简单封装 "& ...

  5. 创建Android Virtual Device

    参考http://book.51cto.com/art/201302/380026.htm Linux版的Android SDK没有提供可视化的AVD Manager管理工具,创建AVD可以使用and ...

  6. HDU 5776 sum (前缀和)

    题意:给定 n 个数,和 m,问你是不是存在连续的数和是m的倍数. 析:考虑前缀和,如果有两个前缀和取模m相等,那么就是相等的,一定要注意,如果取模为0,就是真的,不要忘记了,我当时就没记得.... ...

  7. js ajax post提交 ie和火狐、谷歌提交的编码不一致,导致中文乱码

    今天遇到一个问题找了很久发现: 使用js ajax post提交 ie和火狐.谷歌提交的编码不一致,导致中文乱码 //http://www.cnblogs.com/QGC88 $.ajax({ url ...

  8. C++的辅助工具介绍 [转]

    C++的辅助工具介绍 1 文档类  (1) Doxygen  参考站点:http://www.doxygen.org  Doxygen是一种适合C风格语言(如C++.C.IDL.Java甚至包括C#和 ...

  9. CDocument类的UpdateAllViews()成员函数

    (一)UpdateAllViews() 与 Invalidate()的区别 UpdateAllViews()是在DOC/VIEW结构中,当一个视图的数据改变后,通知所有视图作相应的改变,和重画毫无关系 ...

  10. Using Eclipse With CloudStack

    As part of switching to Apache Maven for building CloudStack, the .classpath and .project files used ...