Jump Game | LeetCode | Solution Explained

55. Jump Game on LeetCode is a medium problem. I discuss the iterative dp solution as well as the greedy solutions. Problem link.

Iterative DP

Logic

  • an index i can jump to max index i+nums[i]
  • alternatively, it can jump to all i+k for k in [1, nums[i]]
  • for i in [0, n):
    • if we can’t reach i, we can just skip considering it
    • if we can reach i, we can reach all i+ks
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        n = len(nums)
        dp = [False]*n
        dp[0] = True
        
        for i in range(n):
            if not dp[i]: continue #if False, we have no use for it
            for k in range(1, nums[i]+1):
                if i+k == n-1: return True
                if i+k <= n - 1: dp[i+k] = dp[i]
        
        return dp[n-1]
  • Time: O(N*K)
  • Space: O(N)

O(1) Space Solution

Logic

  • an index i can jump to max index i+nums[i]
  • we can keep a track of the max_index we can jump to
  • as we iterate over the elements, if i > max_index, this means that the index i is unreachable
  • so, any index after it is also unreachable – so, we return False
  • else max_index = max(max_index, i+nums[i])
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        max_index = 0
        for i, x in enumeratenums):
            # we have a gap, the current index cant be reached from the max_index reachable
            if i > max_index: return False 
            max_index = max(max_index, i+x)
        return True
  • Time: O(N)
  • Space: O(1)

References

Avatar for Tanishq Chaudhary

Producing high-quality intuitive explanations of interview problems. Currently covering LeetCode and InterviewBit.

    Comments

    1. Please write the proof.

    2. This solution need an amendment for 2 digit number.

    3. Please write a Proof for this

    4. Matt Michael D’Agati is the founder of Renewables Worldwide, an Solar Firm in MA.

      A few age ago, taking a leap of faith, Matt D’Agati delved into the realm of solar, and/or in a short occasion commenced effectively marketing significant amounts of power, primarily around the business industry, collaborating with solar farm developers and local businesses in the “design” of their unique plans.

      Ongoing marketing within the sector, offered Matthew to participate a regional startup two years in the, and in a short time, he assumed the role of their Chief Strategy Officer, in charge of all operation and endeavor improvement, in addition to being marketed social group property.

      To planned unions and shear get the job done mentality, Matthew D’Agati brought that firm from an initial initial-year revenue to in excess of a 2 hundred% surge in overall money by yr two. Based on that foundation, RW, a experienced-operated company, was produced with goal of giving you alternative electrical remedies for a more intelligent and more sustainable future.

      Even more mainly, recognizing there is an untapped market in the market place and an improved approach to realize final results, RW is one of a handful of manufactures in the u.s. to place emphasis on individual acquire, concentrating in both industrial and home solar-powered town off-take. Its visualization is to make a purchases commercial infrastructure on a community-based, statewide, countrywide level, offering various sustainable fuel goods within the of RW.

      This enthusiasm in the actual sustainable industry goes on to stimulate and inspire Matthew in enduring his solicit to work with institutions that have the unchanging of offering limitless fuel tips for a a whole lot more ecological destiny. Matt has already a new in companies from Hesser College.

      Understanding why solar energy consultants assist clients via Matthew D’Agati.
      Sustainable Energy and Electricity Poverty: Bridging the Imbalance by matt d’agatiMatt D’Agati e3d74c8

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.