wayetan
12/31/2013 - 11:25 AM

Jump Game

Jump Game

// version 1: Dynamic Programming
public class Solution {
    public int jump(int[] A) {
        int[] steps = new int[A.length];
        
        steps[0] = 0;
        for (int i = 1; i < A.length; i++) {
            steps[i] = Integer.MAX_VALUE;
            for (int j = 0; j < i; j++) {
                if (steps[j] != Integer.MAX_VALUE && j + A[j] >= i) {
                    steps[i] = steps[j] + 1;
                    break;
                }
            }
        }
        
        return steps[A.length - 1];
    }
}


// version 2: Greedy
public class Solution {
    public int jump(int[] A) {
        if (A == null || A.length == 0) {
            return -1;
        }
        int start = 0, end = 0, jumps = 0;
        while (end < A.length - 1) {
            jumps++;
            int farthest = end;
            for (int i = start; i <= end; i++) {
                if (A[i] + i > farthest) {
                    farthest = A[i] + i;
                }
            }
            start = end + 1;
            end = farthest;
        }
        return jumps;
    }
}
public class Solution {
    public int jumpDPLeftToRight(int[] A) {
    	
		int n = A.length;
		int[] jumps = new int[n]; // jumps[n-1] will hold the result
		int i, j;

		if (n == 0 || A[0] == 0)
			return Integer.MAX_VALUE;

		jumps[0] = 0;

		// Find the minimum number of jumps to reach arr[i]
		// from arr[0], and assign this value to jumps[i]

		for (i = 1; i < n; i++) {
			jumps[i] = Integer.MAX_VALUE;

			for (j = 0; j < i; j++) {
				if (i <= j + A[j] && jumps[j] != Integer.MAX_VALUE) {
					jumps[i] = jumps[j] + 1;

					break;
				}
			}
		}
		return jumps[n - 1];
	}

	public static int jumpDPRightToLeft(int[] A) {
		int n = A.length;
		int[] jumps = new int[n]; // jumps[0] will hold the result
		int min;

		// Minimum number of jumps needed to reach last element
		// from last elements itself is always 0
		jumps[n - 1] = 0;

		int i, j;

		// Start from the second element, move from right to left
		// and construct the jumps[] array where jumps[i] represents
		// minimum number of jumps needed to reach arr[m-1] from arr[i]
		for (i = n - 2; i >= 0; i--) {
			// If arr[i] is 0 then arr[n-1] can't be reached from here
			if (A[i] == 0)
				jumps[i] = Integer.MAX_VALUE;

			// If we can direcly reach to the end point from here then
			// jumps[i] is 1
			else if (A[i] >= n - i - 1)
				jumps[i] = 1;

			// Otherwise, to find out the minimum number of jumps needed
			// to reach arr[n-1], check all the points reachable from here
			// and jumps[] value for those points
			else {
				min = Integer.MAX_VALUE; // initialize min value

				// following loop checks with all reachable points and
				// takes the minimum
				for (j = i + 1; j < n && j <= A[i] + i; j++) {
					if (min > jumps[j])
						min = jumps[j];
				}

				// Handle overflow
				if (min != Integer.MAX_VALUE)
					jumps[i] = min + 1;
				else
					jumps[i] = min; // or INT_MAX
			}
		}

		return jumps[0];
	}
}
/**
 * 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.)
 */
 
public class Solution {
    public int jump(int[] A) {
        int n = A.length;
        int max = 0, curr_max = 0, steps = 0;
        int start = 0;
        while(start < n){
            // jump out of the range, achieve the goal! 
            if(max >= n - 1)
                break;
            // move to the current covered range, and start with a new index
            while(start <= max){
                curr_max = Math.max(curr_max, start + A[start]);
                start += 1;
            }
            // take the step and record the current max
            steps++;
            max = curr_max;
        }
        return steps;
    }
}
/**
 * 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.
 */
 
public class Solution {
    public boolean canJump(int[] A) {
        int maxCover = 0;
        for(int start = 0; start <= maxCover && start < A.length; start++){
            maxCover = Math.max(A[start] + start, maxCover);
            if(maxCover >= A.length - 1)
                return true;
        }
        return false;
    }
    
    public boolean canJumpDP(int[] A) {
      boolean[] can = new boolean[A.length];
      // Initialization
      can[0] = true;
        
      for (int i = 1; i < A.length; i++) {
          for (int j = 0; j < i; j++) {
              if (can[j] && j + A[j] >= i) {
                  can[i] = true;
                  break;
              }
          }
      }
        
      return can[A.length - 1];
    }
}