Jump Game II

Question

  1. Given an array of non-negative integers,
  2. you are initially positioned at the first index of the array.
  3. Each element in the array represents your maximum jump length at that position.
  4. Your goal is to reach the last index in the minimum number of jumps.
  5. Example
  6. Given array A = [2,3,1,1,4]
  7. The minimum number of jumps to reach the last index is 2.
  8. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

题解(自顶向下-动态规划)

首先来看看使用动态规划的解法,由于复杂度较高在A元素较多时会出现TLE,因为时间复杂度接近 O(n^3). 工作面试中给出动规的实现就挺好了。

  1. State: f[i] 从起点跳到这个位置最少需要多少步
  2. Function: f[i] = MIN(f[j]+1, j < i && j + A[j] >= i) 取出所有能从j到i中的最小值
  3. Initialization: f[0] = 0,即一个元素时不需移位即可到达
  4. Answer: f[n-1]

C++ Dynamic Programming

  1. class Solution {
  2. public:
  3. /**
  4. * @param A: A list of lists of integers
  5. * @return: An integer
  6. */
  7. int jump(vector<int> A) {
  8. if (A.empty()) {
  9. return -1;
  10. }
  11. const int N = A.size() - 1;
  12. vector<int> steps(N, INT_MAX);
  13. steps[0] = 0;
  14. for (int i = 1; i != N + 1; ++i) {
  15. for (int j = 0; j != i; ++j) {
  16. if ((steps[j] != INT_MAX) && (j + A[j] >= i)) {
  17. steps[i] = steps[j] + 1;
  18. break;
  19. }
  20. }
  21. }
  22. return steps[N];
  23. }
  24. };

源码分析

状态转移方程为

  1. if ((steps[j] != INT_MAX) && (j + A[j] >= i)) {
  2. steps[i] = steps[j] + 1;
  3. break;
  4. }

其中break即体现了MIN操作,最开始满足条件的j即为最小步数。

题解(贪心法-自底向上)

使用动态规划解Jump Game的题复杂度均较高,这里可以使用贪心法达到线性级别的复杂度。

贪心法可以使用自底向上或者自顶向下,首先看看我最初使用自底向上做的。对A数组遍历,找到最小的下标min_index,并在下一轮中用此min_index替代上一次的end, 直至min_index为0,返回最小跳数jumps。以下的实现有个 bug,细心的你能发现吗?

C++ greedy from bottom to top, bug version

  1. class Solution {
  2. public:
  3. /**
  4. * @param A: A list of lists of integers
  5. * @return: An integer
  6. */
  7. int jump(vector<int> A) {
  8. if (A.empty()) {
  9. return -1;
  10. }
  11. const int N = A.size() - 1;
  12. int jumps = 0;
  13. int last_index = N;
  14. int min_index = N;
  15. for (int i = N - 1; i >= 0; --i) {
  16. if (i + A[i] >= last_index) {
  17. min_index = i;
  18. }
  19. if (0 == min_index) {
  20. return ++jumps;
  21. }
  22. if ((0 == i) && (min_index < last_index)) {
  23. ++jumps;
  24. last_index = min_index;
  25. i = last_index - 1;
  26. }
  27. }
  28. return jumps;
  29. }
  30. };

源码分析

使用jumps记录最小跳数,last_index记录离终点最远的坐标,min_index记录此次遍历过程中找到的最小下标。

以上的bug在于当min_index为1时,i = 0, for循环中仍有—i,因此退出循环,无法进入if (0 == min_index)语句,因此返回的结果会小1个。

C++ greedy, from bottom to top

  1. class Solution {
  2. public:
  3. /**
  4. * @param A: A list of lists of integers
  5. * @return: An integer
  6. */
  7. int jump(vector<int> A) {
  8. if (A.empty()) {
  9. return 0;
  10. }
  11. const int N = A.size() - 1;
  12. int jumps = 0, end = N, min_index = N;
  13. while (end > 0) {
  14. for (int i = end - 1; i >= 0; --i) {
  15. if (i + A[i] >= end) {
  16. min_index = i;
  17. }
  18. }
  19. if (min_index < end) {
  20. ++jumps;
  21. end = min_index;
  22. } else {
  23. // cannot jump to the end
  24. return -1;
  25. }
  26. }
  27. return jumps;
  28. }
  29. };

源码分析

之前的 bug version 代码实在是太丑陋了,改写了个相对优雅的实现,加入了是否能到达终点的判断。在更新min_index的内循环中也可改为如下效率更高的方式:

  1. for (int i = 0; i != end; ++i) {
  2. if (i + A[i] >= end) {
  3. min_index = i;
  4. break;
  5. }
  6. }

题解(贪心法-自顶向下)

看过了自底向上的贪心法,我们再来瞅瞅自顶向下的实现。自顶向下使用farthest记录当前坐标出发能到达的最远坐标,遍历当前startend之间的坐标,若i+A[i] > farthest时更新farthest(寻找最小跳数),当前循环遍历结束时递推end = farthestend >= A.size() - 1时退出循环,返回最小跳数。

C++

  1. /**
  2. * http://www.jiuzhang.com/solutions/jump-game-ii/
  3. */
  4. class Solution {
  5. public:
  6. /**
  7. * @param A: A list of lists of integers
  8. * @return: An integer
  9. */
  10. int jump(vector<int> A) {
  11. if (A.empty()) {
  12. return 0;
  13. }
  14. const int N = A.size() - 1;
  15. int start = 0, end = 0, jumps = 0;
  16. while (end < N) {
  17. int farthest = end;
  18. for (int i = start; i <= end; ++i) {
  19. if (i + A[i] >= farthest) {
  20. farthest = i + A[i];
  21. }
  22. }
  23. if (end < farthest) {
  24. ++jumps;
  25. start = end + 1;
  26. end = farthest;
  27. } else {
  28. // cannot jump to the end
  29. return -1;
  30. }
  31. }
  32. return jumps;
  33. }
  34. };