[Leetcode 56] 55 Jump Game

Problem:

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.

 

Analysis:

At first glance, it seems like a backtracking problem. But won't pass the large test cases.

Then try to use simulation method, though can go further than backtracking, still can pass all test cases.

The developed a one-scan algorithm. Observe that we can compute what's the furthest position we can go at each position. If the current position is less than the max position we can go, then it means that we can reach this place. And if from this place go to the furthest place we can go is greater than max. Then it means that the range is extended. We can continue this process until 1. reach the end of the array; 2. find the max covers the final place. Since we only need to scan the array only once, the total time complexity is O(n)

 

Code:

Backtracking Solution: TLE

 1 class Solution {
 2 public:
 3     int len;
 4     int* array;
 5     
 6     bool canJump(int A[], int n) {
 7         // Start typing your C/C++ solution below
 8         // DO NOT write int main() function
 9         len = n;
10         array = A;
11         bool res = false;
12         
13         bc(0, res);
14         return res;
15     }
16     
17     void bc(int pos, bool &res) {
18         if (pos+1 > len)
19             return ;
20         
21         if (pos+1 == len) {
22             res = true;
23             return ;
24         }
25         
26         for (int i=0; i<=array[pos]; i++)
27            bc(pos+i, res);
28         
29         return ;
30     }
31 };
View Code

 

Iterative Solution: TLE

 1 class Solution {
 2 public:
 3 
 4     bool canJump(int A[], int n) {
 5         // Start typing your C/C++ solution below
 6         // DO NOT write int main() function
 7         if (n==0) return false;
 8         
 9         bool tag[n];
10         
11         tag[0] = true;
12         for (int i=1; i<n; i++) 
13             tag[i] = false;
14             
15         for (int i=0; i<n; i++) {
16             if (tag[i] == true) {
17                 for (int j=A[i]; j>0; j--) {
18                     if (i+j < n) 
19                         tag[i+j] = true;
20                     
21                     if (tag[n-1] == true)
22                         return true;
23                 }
24             }
25         }
26         
27         return tag[n-1];
28     }
29 };
View Code

 

 O(n) solution:

 1 class Solution {
 2  public:
 3  
 4      bool canJump(int A[], int n) {
 5          // Start typing your C/C++ solution below
 6          // DO NOT write int main() function
 7          if (n==0) return false;
 8          
 9          int max = 1+A[0];
10          for (int i=2; i<+n; i++) {
11              if (i<=max && (i+A[i-1]) > max) {
12                  max = i+A[i-1];
13              }
14              
15              if (max >= n)
16                 break;
17          }
18          
19          return (max >= n);
20      }
21  };
View Code

 

转载于:https://www.cnblogs.com/freeneng/archive/2013/05/26/3099613.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值