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

如题,求到达末位最少的跳的次数。

 class Solution {
private:
int dp[];
public:
int jump(vector<int>& nums) {
int maxPos = ;
dp[] = ;
int pos = ;
int sz = nums.size();
for(int i = ; i <= maxPos; ++i){
pos = i + nums[i];
if(pos >= sz)
pos = sz - ;
if(pos > maxPos){
for(int j = maxPos + ; j <= pos; ++j)
dp[j] = dp[i] + ;
maxPos = pos;
} if(maxPos == sz - )
return dp[sz - ];
} }
};

LeetCode OJ:Jump Game II(跳跃游戏2)的更多相关文章

  1. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

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

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

  3. [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)

    https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...

  4. 【LeetCode每天一题】Jump Game II(跳跃游戏II)

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

  5. [Leetcode] jump game ii 跳跃游戏

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

  6. LeetCode 55. Jump Game (跳跃游戏)

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

  7. 045 Jump Game II 跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...

  8. [LeetCode] 45. Jump Game II 跳跃游戏之二

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

  9. 【leetcode】Jump Game I, II 跳跃游戏一和二

    题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...

  10. Leetcode力扣45题 跳跃游戏 II

    原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...

随机推荐

  1. 阿里云ECS服务器磁盘挂载(转)

    买了阿里云的ECS云服务器,本机赠送20GB的磁盘,感觉不够用,又买了一块500GB的磁盘,本文就是记录怎么把这500GB的磁盘挂载上. 检查现在磁盘情况 我们可以看到买的那个500GB的磁盘没有出现 ...

  2. C++之map使用

    解析文件或者字符串,一key跟keyvalue来存在map中,如下代码: test.h: #include <map>#include <vector> Class test ...

  3. java之继承中的构造方法

    继承中的构造方法  1.子类的构造过程中必须调用其基类的构造方法. 2.子类可以在自己的构造方法中使用super(argument_list)调用基类的构造方法. 2.1.使用this(argumen ...

  4. 关于axios

    简介 axios是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中 主要是用于向后台发起请求的,还有在请求中做更多是可控功能. 特点 从浏览器中创建 XMLHttpRe ...

  5. flume从log4j收集日志输出到kafka

    1. flume安装 (1)下载:wget http://archive.cloudera.com/cdh5/cdh/5/flume-ng-1.6.0-cdh5.7.1.tar.gz (2)解压:ta ...

  6. 无线网卡在 MAC 系统下的安装与使用过程

    MAC系统安装netgear无线网卡的方法: 1)去网件官网下载相应的驱动软件 2)单击页面左侧的“Version 1.0.0.0”进入下载页面如下图 3)选择对应您系统版本的驱动程序,按右键保存到计 ...

  7. 何为K-邻近算法

    答:K-邻近算法,英文为K-nearest neighbor(KNN),就是计算要测试对象与k个样本对象之间的距离,通过距离的大小来对测试对象进行分类

  8. LeetCode——minimum-path-sum

    Question Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  9. eclipse不能添加tomcat7的问题

    问题如下: 解决问题: 1.把eclipse先关了 2.把eclipse的工作空间的两个文件删除 org.eclipse.jst.server.tomcat.core.prefs和org.eclips ...

  10. linux下增加useradd提示existing lock file /etc/subgid.lock without a PID

    # useradd git -g git useradd: existing lock file /etc/subgid.lock without a PID useradd: cannot lock ...