【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 ...
随机推荐
- 【DM642学习笔记四】flash烧写过程——错误记录…
(欢迎批评指正) 一,打开.cdd配置文件时出错: 解决:在FlashBurn配置窗口中,Conversion Cmd一栏可不用管: 菜单Program—Download FBTC,load ...
- idea长期使用
0. 如果你的idea(版本2019.02)是已过期状态则先上网找个激活码激活再进行下面步骤延长使用期至2089年 1. 附件下载地址: 链接: https://pan.baidu.com/s/1Tp ...
- js数组快速排序/去重
数组的排序 快速排序 思路: (1)在数据集之中,选择一个元素作为”基准”(pivot). (2)所有小于”基准”的元素,都移到”基准”的左边:所有大于”基准”的元素,都移到”基准”的右边. (3) ...
- spring定时任务scheduler集群环境下指定运行服务器防止多服务器多次执行
使用spring的@Scheduler注解可以非常方便的启动一个定时任务,但是当服务部署在多台服务器上做负载均衡的时候,可能会出现重复执行的情况. 现在我们通过代码指定job只在某一台机器执行. 首先 ...
- Redis学习03——存储字符串(String)
--------------------- 作者:愤怒的小明 来源:CSDN 原文:https://blog.csdn.net/qiwenmingshiwo/article/details/78118 ...
- 2018-8-14-Resharper-如何把类里的类移动到其他文件
title author date CreateTime categories Resharper 如何把类里的类移动到其他文件 lindexi 2018-08-14 17:34:39 +0800 2 ...
- 【python之路41】web框架
一.web框架介绍 一共有两种web框架 1.既包含socket又能逻辑处理 tornado框架 2.只能处理逻辑 Django bottle flask 二.web框架的本质 众所周知,对于所有的 ...
- web前端学习(四)JavaScript学习笔记部分(9)-- JavaScript面向对象详解
1.认识面向对象 1.1.概念 1.一切事物皆是对象 2.对象具有封装和继承特性 3.信息隐藏(类的信息隐藏,包括属性和方法) <!DOCTYPE html> <html lang= ...
- 20190716-T1-礼物
呵呵,我暴力WA了 这个题充分考验了大家对数学的理解(麦蒙大多在胡诌) 但是确实如此啊. 看数据范围想状压.(我额嗯嗯想到暴力?) 然后设出一个可爱的$dp$式(主语当然不是我,是出题人大佬) $f_ ...
- Hackerrank--Stock Maximize(DP Practice)
题目链接 Your algorithms have become so good at predicting the market that you now know what the share p ...