DP reversely thinking, easy to understand
293
public static int jump2(int[] nums) {
    if (nums.length <= 1) {
        return 0;
    }
    int[] minStepsLeftToEnd = new int[nums.length-1];
    Arrays.fill(minStepsLeftToEnd, 20000);

    for (int i = nums.length-2; i >= 0 ; i--) {  // Check from tail
        for (int j = nums[i]; j > 0; j--) { // Check every nextIndex that the current index can reach (from large to small)
            if (i + j >= nums.length-1) {  // If it can reach to the end, it means that only one step left to the end at the index
                minStepsLeftToEnd[i] = 1; 
				break; // stop earlier
            } else {
				// if it cannot reach end, update the "step left" with the possible reachable index and find out the min possible step left
                minStepsLeftToEnd[i] = Math.min(minStepsLeftToEnd[i], minStepsLeftToEnd[i+j] + 1); 
            }
        }
    }
    return minStepsLeftToEnd[0];
}
Comments (0)