【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 ...
随机推荐
- JDK、Eclipse、Tomcat、Maven、IDEA 常见问题
windows操作分成了32位和64位的系统,不同的系统安装的软件也不一样. 查询电脑操作系统是多少位? J D K 01. 下载安装 02. 目录解释 03. 配置环境变量 (JDK安装成功后进行配 ...
- Vue. 之 刷新当前页面,重载页面数据
Vue. 之 刷新当前页面,重载页面数据 如下截图,点击左侧不同的数据,右侧根据左侧的KEY动态加载数据.由于右侧是同一个页面,在进行路由跳转后,不会再次刷新数据. 解决方案: 右侧的页面中 scri ...
- pytorch 加载训练好的模型做inference
前提: 模型参数和结构是分别保存的 1. 构建模型(# load model graph) model = MODEL() 2.加载模型参数(# load model state_dict) mode ...
- Django项目:CRM(客户关系管理系统)--30--22PerfectCRM实现King_admin数据添加
登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html # king_urls.py # ————————02PerfectCRM创建ADMIN页面—— ...
- CF549G Happy Line
传送门 解题思路 题意大概就是给你个数列,你可以随意交换i,i+1,交换后位于第i+1位的数字+1,位于第i位的数字-1,问最终能否形成一个不下降序列并输出.设初始数列中两个位置x,y最终交换后的位置 ...
- 使用netbeans 搭建maven工程 整合spring springmvc框架
使用netbeans7.4 自带的tomcat7.0 所以jdk选择7.xx 然后等待生成空的工程,会缺一些文件夹,和文件,后续需要的时候补齐 然后修改pom.xml添加引用,直接覆盖dependen ...
- Win7。56个进程让我头疼
乱七八糟的进程一个一个往外蹦,如此痛苦. 安装了一个VM9,进程数量+5,安装了卖咖啡的,进程数量+5. 除去这10个,系统进程数量还有46个....还是太多... 64位系统,真的很痛苦,还没有怎么 ...
- 数据库完整性 ch.5
数据库的完整性 是指 数据的正确性(correctness) 和 相容性 (compat-ability) 5.1 实体完整性 定义 对单属性码的说明有两种方法,一种是定义为表约束条件,一种是定义为列 ...
- Django基础内容整理
- 火狐下button标签子元素无法点击
button下元素点击事件:在chrome和safari下每个a标签可以点击,在火狐下a标签无法点击. <button> <a href="javascript:;&quo ...