Jump Game II

这一题比较容易想到的思路是动态规划,因为直接从前往后分析发现,每次一个结点能到达后面多个节点的时候不知道选哪个,就只能递归,这样会造成很多重复的子问题,于是发现可以倒过来,遍历,这样每次一个节点可以抵达的前方的结点到目的地的距离都是确定的,所以可解。但是正如很多的动态规划算法一样,这个算法的时间复杂度是n^2的,对于大数组很快就超时了。

这题一个比较好的思路就是贪心算法。

这个名词一点出来,就可以按照常规的贪心算法的思路去套用了,当然最重要的就是要证明这里有最优子结构。我们可以发现,仍旧倒过来遍历。例如,从最后一个点开始,一般会有很多点能到达最后一个点,我们认为在这些点中最左边的一个点是最优解必经的一个点(若不只一个最优解,至少有最优解经过它)。

为什么呢? 例如,有 A B G H Y 五个点(从左至右排列)都能到达目的地,那么A点必在某个最优解的路径上。因为前面的点,如果能到达G H Y之类的,它就肯定能到达A点,反之却未必。

int jump(int A[], int n){
	if (n == 0)
		return 0;
	int index = n - 1;
	int step = 0;
	while (1){
		step++;
		int k = index;
		for (int i = k - 1; i >= 0; i--) {
			if (i + A[i] >= k)
				index = i;
		}
		if (index == 0)
			break;
	}
	return step;
}

  

时间: 2024-08-15 07:56:33

Jump Game II的相关文章

Jump Game II leetcode java

题目: 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 j

Jump Game II (leetcode) DP的两种思路

第一种思路是: dp(i):到位置i所需要的最少步数 dp(i)一定是递增的,所以从j=A[i]开始(从最远的位置开始),更新数组直到dp(j+i) <= dp(i) + 1为止 如果去掉,会TLE int jump(int A[], int n) { int* dp = new int[n];//dp[i]到i所需的最小步数 memset(dp, 0x3f, sizeof(int) * n); dp[0] = 0; for (int i = 0; i < n; i++) { for (int

Jump Game II &lt;LeetCode&gt;

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

45. Jump Game II Leetcode Python

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

[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

LeetCode: Jump Game II [044]

Perface 如果让你实现这个页面和一些操作的,比如点击1.2.3等就在那个input text中显示,还有删除功能,拨打我们先不要管它,只是模拟而已.要是我刚开始做的话,我会这样做: 用css.HTML布局那个界面 用javascript的事件委托监听那个按钮的父节点的点击事件 但是如果我想用面向对象的思想做呢?我是用Ext做的,所以我想说的是它帮我封装了很多.可能一些没用过Ext的人不太了解我下面贴的代码,但是我会尽量解释清楚的! Description ContactTelPanel =

【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

[leetcode]Jump Game II @ Python

原题地址:https://oj.leetcode.com/problems/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 i

【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

LeetCode: Jump Game II 解题报告

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 nu