Question: (116) Jump Game
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.
Determine if you are able to reach the last index.
Example
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
f[i] = OR (f[j], j < i ~\&\&~ j + A[j] \geq i)
, 状态 j 转移到 i, 所有小于i的下标j的元素中是否存在能从j跳转到i得这种自顶向下的方法需要使用额外的 O(n) 空间,保存小于N-1时的状态。且时间复杂度在恶劣情况下有可能变为 12+22+⋯+n2=O(n3), 出现 TLE 无法AC的情况,不过工作面试能给出这种动规的实现就挺好的了。
C++ from top to bottom
class Solution {
public:
/**
* @param A: A list of integers
* @return: The boolean answer
*/
bool canJump(vector<int> A) {
if (A.empty()) {
return true;
}
vector<bool> jumpto(A.size(), false);
jumpto[0] = true;
for (int i = 1; i != A.size(); ++i) {
for (int j = i - 1; j >= 0; --j) {
if (jumpto[j] && (j + A[j] >= i)) {
jumpto[i] = true;
break;
}
}
}
return jumpto[A.size() - 1];
}
};
题意为问是否能从起始位置到达最终位置,我们首先分析到达最终位置的条件,从坐标i出发所能到达最远的位置为 f[i]=i+A[i],如果要到达最终位置,即存在某个 i 使得f[i]≥N−1, 而想到达 i, 则又需存在某个 j 使得 f[j]≥i−1. 依此类推直到下标为0.
以下分析形式虽为动态规划,实则贪心法!
true
true
的元素为 A.size() - 1
true
返回true
C++ greedy, from bottom to top
class Solution {
public:
/**
* @param A: A list of integers
* @return: The boolean answer
*/
bool canJump(vector<int> A) {
if (A.empty()) {
return true;
}
int index_true = A.size() - 1;
for (int i = A.size() - 1; i >= 0; --i) {
if (i + A[i] >= index_true) {
index_true = i;
}
}
return 0 == index_true ? true : false;
}
};
针对上述自顶向下可能出现时间复杂度过高的情况,下面使用贪心思想对i进行递推,每次遍历A中的一个元素时更新最远可能到达的元素,最后判断最远可能到达的元素是否大于 A.size() - 1
C++ greedy, from top to bottom
class Solution {
public:
/**
* @param A: A list of integers
* @return: The boolean answer
*/
bool canJump(vector<int> A) {
if (A.empty()) {
return true;
}
int farthest = A[0];
for (int i = 1; i != A.size(); ++i) {
if ((i <= farthest) && (i + A[i] > farthest)) {
farthest = i + A[i];
}
}
return farthest >= A.size() - 1;
}
};
Question: (117) 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.
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.)
首先来看看使用动态规划的解法,由于复杂度较高在A元素较多时会出现TLE,因为时间复杂度接近 O(n3). 工作面试中给出动规的实现就挺好了。
C++ Dynamic Programming
class Solution {
public:
/**
* @param A: A list of lists of integers
* @return: An integer
*/
int jump(vector<int> A) {
if (A.empty()) {
return -1;
}
const int N = A.size() - 1;
vector<int> steps(N, INT_MAX);
steps[0] = 0;
for (int i = 1; i != N + 1; ++i) {
for (int j = 0; j != i; ++j) {
if ((steps[j] != INT_MAX) && (j + A[j] >= i)) {
steps[i] = steps[j] + 1;
break;
}
}
}
return steps[N];
}
};
状态转移方程为
if ((steps[j] != INT_MAX) && (j + A[j] >= i)) {
steps[i] = steps[j] + 1;
break;
}
其中break即体现了MIN操作,最开始满足条件的j即为最小步数。
使用动态规划解Jump Game的题复杂度均较高,这里可以使用贪心法达到线性级别的复杂度。
贪心法可以使用自底向上或者自顶向下,首先看看我最初使用自底向上做的。对A数组遍历,找到最小的下标min_index
,并在下一轮中用此min_index
替代上一次的end
, 直至min_index
为0,返回最小跳数jumps
。以下的实现有个 bug,细心的你能发现吗?
C++ greedy from bottom to top, bug version
class Solution {
public:
/**
* @param A: A list of lists of integers
* @return: An integer
*/
int jump(vector<int> A) {
if (A.empty()) {
return -1;
}
const int N = A.size() - 1;
int jumps = 0;
int last_index = N;
int min_index = N;
for (int i = N - 1; i >= 0; --i) {
if (i + A[i] >= last_index) {
min_index = i;
}
if (0 == min_index) {
return ++jumps;
}
if ((0 == i) && (min_index < last_index)) {
++jumps;
last_index = min_index;
i = last_index - 1;
}
}
return jumps;
}
};
使用jumps记录最小跳数,last_index记录离终点最远的坐标,min_index记录此次遍历过程中找到的最小下标。
以上的bug在于当min_index为1时,i = 0, for循环中仍有--i,因此退出循环,无法进入if (0 == min_index)
语句,因此返回的结果会小1个。
C++ greedy, from bottom to top
class Solution {
public:
/**
* @param A: A list of lists of integers
* @return: An integer
*/
int jump(vector<int> A) {
if (A.empty()) {
return 0;
}
const int N = A.size() - 1;
int jumps = 0, end = N, min_index = N;
while (end > 0) {
for (int i = end - 1; i >= 0; --i) {
if (i + A[i] >= end) {
min_index = i;
}
}
if (min_index < end) {
++jumps;
end = min_index;
} else {
// cannot jump to the end
return -1;
}
}
return jumps;
}
};
之前的 bug version 代码实在是太丑陋了,改写了个相对优雅的实现,加入了是否能到达终点的判断。在更新min_index
的内循环中也可改为如下效率更高的方式:
for (int i = 0; i != end; ++i) {
if (i + A[i] >= end) {
min_index = i;
break;
}
}
看过了自底向上的贪心法,我们再来瞅瞅自顶向下的实现。自顶向下使用farthest
记录当前坐标出发能到达的最远坐标,遍历当前start
与end
之间的坐标,若i+A[i] > farthest
时更新farthest
(寻找最小跳数),当前循环遍历结束时递推end = farthest
。end >= A.size() - 1
时退出循环,返回最小跳数。
/**
* http://www.jiuzhang.com/solutions/jump-game-ii/
*/
class Solution {
public:
/**
* @param A: A list of lists of integers
* @return: An integer
*/
int jump(vector<int> A) {
if (A.empty()) {
return 0;
}
const int N = A.size() - 1;
int start = 0, end = 0, jumps = 0;
while (end < N) {
int farthest = end;
for (int i = start; i <= end; ++i) {
if (i + A[i] >= farthest) {
farthest = i + A[i];
}
}
if (end < farthest) {
++jumps;
start = end + 1;
end = farthest;
} else {
// cannot jump to the end
return -1;
}
}
return jumps;
}
};