Short Python Solution


  • 0
    J
    class Solution(object):
        def search(self, stones, cur_pos, last_jump, m):
            if cur_pos == stones[-1]:
                return True
            else:
                for next_pos in xrange(max(cur_pos + last_jump - 1, cur_pos + 1), min(cur_pos + last_jump + 1, stones[-1]) + 1):
                    if next_pos in m and self.search(stones, next_pos, next_pos - cur_pos, m):
                        return True
                return False
            
            
        def canCross(self, stones):
            """
            :type stones: List[int]
            :rtype: bool
            """
            if stones[1] != 1:
                return False
            m = {v : i for i, v in enumerate(stones)}
            return self.search(stones, 1, 1, m)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.