055-jump-game
Question
https://leetcode.com/problems/jump-game/description/
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.
Example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
Thought Process
- Typical process
- Start with the recursive backtracking solution
- Optimize by using a memoization table (top-down dynamic programming)
- Remove the need for recursion (bottom-up dynamic programming
- Apply final tricks to reduce the time / memory complexity
- Time complexity O(2^n), O(n^2), O(n^2)
- Space complexity O(n), O(n), O(n)
- After bottom up approach we can see that all we care about is the first reachable index/left most good index
- Instead of using the array to keep track of the index, we update the good index whenever current index + its num is greater than or equal to the good index
- Time complexity O(n)
- Space complexity O(1)
Solution
Bottom up
class Solution {
public boolean canJump(int[] nums) {
int n = nums.length;
boolean[] canReach = new boolean[n];
canReach[n - 1] = true;
for (int i = n - 2; i >=0 ; i--) {
int bound = Math.min(nums[i] + i, n - 1);
for (int j = i + 1; j <= bound; j++) {
if (canReach[j]) {
canReach[i] = true;
break;
}
}
}
return canReach[0];
}
}
Greedy
class Solution {
public boolean canJump(int[] nums) {
int n = nums.length;
int goodId = n - 1;
for (int i = n - 2; i >=0 ; i--) {
if (nums[i] + i >= goodId) goodId = i;
}
return goodId == 0;
}
}