【To Read】LeetCode | 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.)
思路:
第一种思路是利用迭代的思路来计算最小跳数,但是时间复杂度比较大;第二种思路是反过来想,要达到最后一条,倒数第二条至少应该到哪个位置,以此类推直到我们倒推到第一位时便可知最小跳数;第三种思路是用动态规划DP的观点来实现。DP[i]代表到达i的最小跳数,显然DP是一个递增的数组。每次循环只需要尽量找到最小的DP[k],使其满足k+A[k]>=n。
代码:
思路一:迭代(时间复杂度不满足要求)
- class Solution {
- public:
- int jump(int A[], int n) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- int* v = new int[1];
- v[0] = INT_MAX;
- jumpRepeat(A, 0, n-1, 0, v);
- if(v[0] == INT_MAX)
- {
- return 0;
- }
- return v[0];
- }
- void jumpRepeat(int A[], int i, int m, int n,int* v)
- {
- if(i >= m)
- {
- if(v[0] > n)
- {
- v[0] = n;
- }
- return;
- }
- if(A[i] == 0)
- {
- return;
- }
- else
- {
- for(int j = 1; j <= A[i]; j++)
- {
- jumpRepeat(A, i + j, m, n+1, v);
- }
- }
- }
- };
- class Solution {
- public:
- int jump(int A[], int n) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- int pre = 0;
- int cur = n - 1;
- int count = 0;
- while(true)
- {
- if(pre == cur)
- {
- return 0;
- }
- count++;
- pre = cur;
- for(int i = n - 2; i >= 0; i--)
- {
- if(i + A[i] >= pre)
- {
- if(cur > i)
- {
- cur = i;
- }
- }
- }
- if(cur == 0)
- {
- return count;
- }
- };
- }
- };
思路三:动态规划
- class Solution {
- public:
- int* dp;
- int jump(int A[], int n) {
- if(n==0)
- {
- return INT_MAX;
- }
- dp = new int[n];
- dp[0] = 0;
- for(int i=1;i<n;i++)
- {
- dp[i] = INT_MAX;
- }
- for(int i=1;i<n;i++)
- {
- for(int j=0;j<i;j++)
- {
- if(j+A[j]>=i)
- {
- int tmp = dp[j]+1;
- if(tmp < dp[i])
- {
- dp[i] = tmp;
- break;
- }
- }
- }
- }
- return dp[n-1];
- }
- };
【To Read】LeetCode | Jump Game II(转载)的更多相关文章
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II(贪婪算法)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode–jump game II
1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...
- leetcode Jump Game II python
@link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...
- [Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- PAT甲级——A1043 Is It a Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- Python爬虫笔记【一】模拟用户访问之webdriver用户登入——第三次(8)
经过post方法之后,因为有动态的value值所以再此回到用webdriver的解决上,但是在下载图片上会打开新打开一个链接,导致与网页图片不同即验证码同步问题,没办法只能想了一个笨法子,网页截图,唉 ...
- qml获取实际渲染的字体
当设置qml的Text元素的字体时,如果系统中不存在设置的字体,qml会根据匹配算法自动选取系统中存在的一种字体.比如:设置font.family: "微软雅黑",但系统中根本没有 ...
- Vuejs实战项目五:数据列表
1.在EasyMock 中添加数据列表模拟接口 请求url:/suyuan/list 请求方式:get 描述:数据列表 mock.js配置: 例: { "code": 2000, ...
- Leetcode147. Insertion Sort List对链表进行插入排序
对链表进行插入排序. 从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示). 每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中. 插入排序算法: 插入排序是 ...
- loj2322 「清华集训 2017」Hello world!
https://loj.ac/problem/2322 先吐槽一下,sb数据毁我青春败我前程. 首先,一个数开根开不了多少次. 当我们把它开到1的时候,我们以后就不需要开他了,我们可以利用并查集跳过他 ...
- Python之路,Day3- Python基础(转载Alex)
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...
- linux-基础-常用命令
一 Linux的简介 1.1 Linux的概述 Linux是基于Unix的开源免费的操作系统,由于系统的稳定性和安全性几乎成为程序代码运行的最佳系统环境.Linux是由Linus Torvalds(林 ...
- 两张图搞清composer install与update区别 - 今日头条(www.toutiao.com)
composer update : 主要是在开发阶段使用,根据我们在composer.json文件中指定的内容升级项目的依赖包. composer install : 主要是在部署阶段使用,以便在生产 ...
- JS 获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...