您的位置:首页 > 其它

Leetcode | Jump Game I && II

2014-05-18 14:53 309 查看

Jump Game I

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.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

Method I

动态规划。reachable[i]存的是能不能从i跳到n-1。

如果存在1<=j<=A[i],使得reachable[i+j]=true,那么reachable[i]=true;否则reachable[i]=false;

但是这样穷搜的话会TLE。所以需要继续避免一些计算。

如果A[i]<=A[i+1]+1时,只要A[i]不为0,那么i必定能够跳到i+1,i能够跳到的位置,i+1都能跳到,所以reachable[i]=reachable[i+1];

如果A[i]>A[i+1]+1时,因为从i往后A[i + 1]+1都已经在计算reachable[i+1]的时候计算过了。如果此时reachable[i+1]=false,我们就从i往后A[i+1]+2开始跳。

class Solution {
public:
bool canJump(int A[], int n) {
if (n == 0) return true;

vector<bool> reachable(n, false);
reachable[n - 1] = true;

for (int i = n - 2; i >= 0; --i) {
if (A[i] == 0) continue;
if (n - 1 - i <= A[i]) {
reachable[i] = true;
} else {
reachable[i] = reachable[i + 1];
if (reachable[i]) continue;
for (int j = A[i + 1] + 2; j <= A[i]; ++j) {
if (reachable[i + j]) {
reachable[i] = true;
break;
}
}
}
}

return reachable[0];
}
};


以前说的好复杂。。。

class Solution {
public:
bool canJump(int A[], int n) {
if (n <= 1) return true;
vector<bool> dp(n, false);
dp[0] = true;
for (int i = 1; i < n; ++i) {
if (!dp[i - 1]) dp[i] = false;
else {
for (int j = i - 1; j >= 0; --j) {
if (A[j] && A[j] >= i - j) {
dp[i] = true;
break;
}
}
}
}
return dp[n - 1];
}
};


Method II

参考了水中的鱼的算法, 更简单。算出一个最大的覆盖区间maxcover,如果i在这区间里,用它能跳到的最远位置来更新maxcover。

当然你也可以把它理解为动态规划的一种。Code Granker 总结的”局部最优和全局最优解法“的一种。

class Solution {
public:
bool canJump(int A[], int n) {
int maxcover = 0;

for (int i = 0; i < n && i <= maxcover; ++i) {
if (A[i] + i > maxcover) maxcover = A[i] + i;
if (maxcover >= n - 1) return true;
}
return false;
}
};


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.)

Method I

同上,但是要分的情况更多了。76ms accept.

min[i]就是从i跳到n-1的最小步数;

1. A[i] == 0,不可达;

2. n-1-i <= A[i],一步可达;

3. A[i] == 1,min[i] = min[i +1]+1; 先跳到i+1,再从i+1往向跳;

4. A[i] == A[i+1] +1,那么从i+1可以跳到的位置,i也可以一步跳到,所以min[i] = min[i+1];

5. 剩下的情况,就要尝试i能跳到的范围,求最小步数了;

class Solution {
public:
int jump(int A[], int n) {
if (n == 0) return 0;

vector<int> min(n, n + 1);
min[n - 1] = 0;

for (int i = n - 2; i >= 0; --i) {
if (A[i] == 0) continue;
if (n - 1 - i <= A[i]) {
min[i] = 1;
} else if (A[i] == 1) {
min[i] = min[i + 1] + 1;
} else if (A[i] == A[i + 1] + 1) {
min[i] = min[i + 1];
}else {
int m = min[i];
for (int j = 1; j <= A[i]; ++j) {
if (min[i + j] + 1 < m) {
m = min[i + j] + 1;
}
}
min[i] = m;
}
}

return min[0];
}
};


如果之前走到从A[min]到A[i-1]能走到最小,如果此时A[min]也能走到A[i],那么可以直接从min跳到i就是最小步数了。不然还得穷搜一下。

class Solution {
public:
int jump(int A[], int n) {
if (n <= 1) return 0;
vector<int> dp(n, n);
dp[0] = 0;
int minIndex = 0;
for (int i = 1; i < n; ++i) {
if (dp[i - 1] == n) {
dp[i] = n;
} else if (i > 1 && A[minIndex] >= i - minIndex) {
dp[i] = dp[i - 1];
} else {
for (int j = i - 1; j >= 0; --j) {
if (A[j] >= i - j && dp[j] + 1 < dp[i]) {
dp[i] = dp[j] + 1;
minIndex = j;
}
}
}
}

return dp[n - 1];
}
};


Method II

水中的鱼的解法。维护一个最大覆盖区间,计算覆盖区间的个数就是最小步数。

class Solution {
public:
int jump(int A[], int n) {
if (n <= 1) return 0;
int s = 0, e = 0, count = 0;

while (s <= e && e < n) {
count++;
int max = 0;
for (int i = s; i <= e; ++i) {
if (A[i] + i > max) max = A[i] + i;
if (max >= n - 1) return count;
}
s = e + 1;
e = max;
}
return 0;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: