python dp solution using dict


  • 0
    X
    class Solution(object):
        def canCross(self, stones):
            """
            :type stones: List[int]
            :rtype: bool
            """
            d = collections.defaultdict(set)
            c = [1,0,-1]
            d[0].add(1)
            for i in stones[:-1]:
                for j in d[i]:
                    for ch in c:
                        if ch+j>0:
                            d[i+j].add(ch+j)
                        else:
                            break
            return len(d[stones[-1]]) != 0
    

Log in to reply
 

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