What is this python solution that people are getting around 72 ms with?


  • 0
    D

    I've tried optimizing my solution but I can't get it to under 300 ms. Obviously I must be using a slow algorithm, but what is the fast one?

    def canCross(self, stones):
    	d = defaultdict(set)
    	d[0] = set([0])
    	stone_set = set(stones)
    	[
    		d[s+move].add(move) 
    		for s in stones 
    		for k in d[s] 
    		for move in (k-1, k, k+1) 
    		if move and s+move in stone_set
    		]
    	return bool(d[stones[-1]])

Log in to reply
 

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