menu

[LeetCode] 055. Jump Game

Problem (Medium)

055. 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.

Example 1:

  • Input: [2,3,1,1,4]
  • Output: true
  • Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

  • Input: [3,2,1,0,4]
  • Output: false
  • Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

Approach 0: (My Solution)

Idea

Solution from LeetCode

Solution

class Solution1:
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        p, width = 0, nums[0]
        while width or len(nums) == 1:
            if p + width >= len(nums) - 1:
                return True
            if nums[p+1] > width - 1:
                p, width = p + 1, nums[p+1]
            else:
                p, width = p + 1, width - 1
        return  False

Complexity

  • Time: $O()$
  • Space: $O()$

Solution Note [LeetCode]:

This is a dynamic programming question. Usually, solving and fully understanding a dynamic programming problem is a 4 step process:

  • Start with the recursive backtracking solution
  • Optimize by using a memoization table (top-down[2] dynamic programming)
  • Remove the need for recursion (bottom-up dynamic programming)
  • Apply final tricks to reduce the time / memory complexity

All solutions presented below produce the correct result, but they differ in run time and memory requirements.

Approach 1: Backtracking

Idea

This is the inefficient solution where we try every single jump pattern that takes us from the first position to the last. We start from the first position and jump to every index that is reachable. We repeat the process until last index is reached. When stuck, backtrack.

Solution

class Solution1:
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        return self.canJumpFromPosition(0, nums)

    def canJumpFromPosition(self, position, nums):
        if position == len(nums) - 1:
            return True
        furthestJump = min(position + nums[position], len(nums) - 1)
        #for nextPosition in range(position+1, furthestJump+1):
        for nextPosition in range(furthestJump, position, -1):
            if self.canJumpFromPosition(nextPosition, nums):
                return True
        return False

Complexity

  • Time: $O(2^n)$ There are $2^n$ (upper bound) ways of jumpingfrom thye first position to the last, where $n$ is the length of the array nums.
  • Space: $O(n)$

Approach 2: Dynamic Programming Top-down

Idea

Top-down Dynamic Programming can be thought of as optimized backtracking. It relies on the observation that once we determine that a certain index is good / bad, this result will never change. This means that we can store the result and not need to recompute it every time.

Therefore, for each position in the array, we remember whether the index is good or bad. Let’s call this array memo and let its values be either one of: GOOD, BAD, UNKNOWN. This technique is called memoization 3.

An example of a memoization table for input array nums = [2, 4, 2, 1, 0, 2, 0] can be seen in the diagram below. We write G for a GOOD position and B for a BAD one. We can see that we cannot start from indices 2, 3 or 4 and eventually reach last index (6), but we can do that from indices 0, 1, 5 and (trivially) 6.

Index 0 1 2 3 4 5 6
nums 2 4 2 1 0 2 0
memo G G B B B G G

Steps

  1. Initially, all elements of the memo table are UNKNOWN, except for the last one, which is (trivially) GOOD (it can reach itself)
  2. Modify the backtracking algorithm such that the recursive step first checks if the index is known (GOOD / BAD)
    1. If it is known then return True / False
    2. Otherwise perform the backtracking steps as before
  3. Once we determine the value of the current index, we store it in the memo table

Solution

index = {'GOOD': 0, 'BAD': 1, 'UNKNOWN': 2}
class Solution2:
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        memo = [index['UNKNOWN'] for _ in range(len(nums))]
        memo[len(memo)-1] = index['GOOD']
        return self.canJumpFromPosition(0, nums, memo)

    def canJumpFromPosition(self, position, nums, memo):
        if memo[position] != index['UNKNOWN']:
            return True if memo[position] == index['GOOD'] else False
        furthestJump = min(position + nums[position], len(nums)-1)
        for nextPosition in range(position+1, furthestJump+1):
            if self.canJumpFromPosition(nextPosition, nums, memo):
                memo[position] = index['GOOD']
                return True
        memo[position] = index['BAD']
        return False

Complexity

  • Time: $O(n^2)$ where $n$ is the length of the array nums.
  • Space: $O(n)$

Approach 3: Dynamic Programming Bottom-up

Idea

Top-down to bottom-up conversion is done by eliminating recursion. In practice, this achieves better performance as we no longer have the method stack overhead and might even benefit from some caching. More importantly, this step opens up possibilities for future optimization. The recursion is usually eliminated by trying to reverse the order of the steps from the top-down approach.

The observation to make here is that we only ever jump to the right. This means that if we start from the right of the array, every time we will query a position to our right, that position has already be determined as being GOOD or BAD. This means we don’t need to recurse anymore, as we will always hit the memo table.

Solution

class Solution3:
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        memo = [index['UNKNOWN'] for _ in range(len(nums))]
        memo[len(memo)-1] = index['GOOD']
        for i in range(len(nums)-2, -1, -1):
            furthestJump = min(i + nums[i], len(nums) - 1)
            for j in range(i+1, furthestJump+1):
                if memo[j] == index['GOOD']:
                    memo[i] = index['GOOD']
                    break
        return memo[0] == index['GOOD']

Complexity

  • Time: $O(n^2)$ where $n$ is the length of the array nums.
  • Space: $O(n)$

Approach 4: Greedy

Idea

Once we have our code in the bottom-up state, we can make one final, important observation. From a given position, when we try to see if we can jump to a GOOD position, we only ever use one - the first one (see the break statement). In other words, the left-most one. If we keep track of this left-most GOOD position as a separate variable, we can avoid searching for it in the array. Not only that, but we can stop using the array altogether.

Iterating right-to-left, for each position we check if there is a potential jump that reaches a GOOD index (currPosition + nums[currPosition] >= leftmostGoodIndex). If we can reach a GOOD index, then our position is itself GOOD. Also, this new GOOD position will be the new leftmost GOOD index. Iteration continues until the beginning of the array. If first position is a GOOD index then we can reach the last index from the first position.

To illustrate this scenario, we will use the diagram below, for input array nums = [9, 4, 2, 1, 0, 2, 0]. We write G for GOOD, B for BAD and U for UNKNOWN. Let’s assume we have iterated all the way to position 0 and we need to decide if index 0 is GOOD. Since index 1 was determined to be GOOD, it is enough to jump there and then be sure we can eventually reach index 6. It does not matter that nums[0] is big enough to jump all the way to the last index. All we need is one way.

Solution

class Solution4:
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        lastPos = len(nums) - 1
        for i in range(len(nums)-1, -1, -1):
            if i + nums[i] >= lastPos:
                lastPos = i
        return lastPos == 0

Complexity

  • Time: $O(n)$ where $n$ is the length of the array nums.
  • Space: $O(1)$

Conclusion:

The question left unanswered is how should one approach such a question in an interview scenario. I would say “it depends”. The perfect solution is cleaner and shorter than all the other versions, but it might not be so straightforward to figure out.

The (recursive) backtracking is the easiest to figure out, so it is worth mentioning it verbally while warming up for the tougher challenge. It might be that your interviewer actually wants to see that solution, but if not, mention that there might be a dynamic programming solution and try to think how could you use a memoization table. If you figure it out and the interviewer wants you to go for the top-down approach, it will not generally be time to think of the bottom-up version, but I would always mention the advantages of this technique as a final thought in the interview.

Most people are stuck when converting from top-down Dynamic Programming (expressed naturally in recursion) to bottom-up. Practicing similar problems will help bridge this gap.



KF

Comments