2015年8月25日星期二

Lintcode: 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.
Have you met this question in a real interview? 
Yes
Example
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
Note
This problem have two method which is Greedy and Dynamic Programming.
The time complexity of Greedy method is O(n).
The time complexity of Dynamic Programming method is O(n^2).
We manually set the small data set to allow you pass the test in both ways. This is just to let you learn how to use this problem in dynamic programming ways. If you finish it in dynamic programming ways, you can try greedy method to make it accept again.
Tags Expand 

Algorithm reference: http://yucoding.blogspot.com/2013/01/leetcode-question-28-jump-game.html
Note: when use vector as parameters of a function, it is passed by value, not by address.

DP:
use a hashmap hash[] to record an bool array to remember whether each position be arrived
for each element located at a of A from left to right:
      if hash[a] is true, set positions within A[a] is  true;
      if not, break the loop
Check whether the last element is true.

Time: O(n^2)
Space: O(n)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    /**
     * @param A: A list of integers
     * @return: The boolean answer
     */
    bool canJump(vector<int> A) {
        // write you code here
        if(A.size() == 0) return false;
        vector<bool> record(A.size(), false);
        record[0] = true;
        for(int i = 0; i < A.size(); i++){
            if(record[i]){
                for(int j = 0; j < A[i]; j++){
                    record[i + j + 1] = true;
                }
            }
            else break;
        }
        return record[A.size() - 1];
    }
};

Greedy:
From the DP, we can easily see that there is a boundary in record array, all positions before that boundary is true, after that is false. So we just need to record the boundary ---- the max position that all elements before current elements can arrive.

Time: O(n)
Space: O(1)

sudo codes:
int mIdx = 0;
For ith element of array A
        if(i > mIdx) return false;
        else mIdx = max(i + A[i], mIdx);
return (A.size() - 1 <= mIdx)? true: false;


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    /**
     * @param A: A list of integers
     * @return: The boolean answer
     */
    bool canJump(vector<int> A) {
        // write you code here
        if(A.size() == 0) return false;
        int mIdx = 0;
        for(int i = 0; i < A.size(); i++){
            if(i > mIdx) break;
            mIdx = max(i + A[i], mIdx);
        }
        return A.size() - 1 <= mIdx ? true: false;
    }
};


Lintcode second:
O(n) dp solution:
dp[i] -- farthest index the ith index can jump to
if(i <= dp[i - 1]) dp[i] = max(dp[i - 1], i + A[i])


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    /**
     * @param A: A list of integers
     * @return: The boolean answer
     */
    bool canJump(vector<int> A) {
        // write you code here
        if(A.size() <= 1) return true;
        vector<int> dp(A.size());
        dp[0] = A[0];
        for(int i = 1; i < A.size(); i++){
            if(dp[i - 1] >= i) dp[i] = max(dp[i - 1], i + A[i]);
            else return false;
        }
        if(dp[A.size() - 1] >= A.size() - 1) return true;
        return false;
    }
};

没有评论:

发表评论