Python Solution


  • 0

    Not the best solution, there are better :P

    class Solution(object):
        def isBetween(self, t, a, b):
            return (a <= t and t <= b) or (b <= t and t <= a)
            
        def isSelfCrossing(self, x):
            dir, d = [1,-1,-1,1], 0
            prev, prevDist = collections.deque([0 for i in range(3)]), 0
            for dist in x:
                coord = prev[1] + prevDist
                prevDist = dist*dir[d%4]
                prev.appendleft(coord)
                d += 1
                if len(prev) < 7:
                    continue
                # Check for intersection
                if self.isBetween(coord, prev[2], prev[4]) and self.isBetween(prev[3], prev[1], prev[1]+prevDist):
                    return True
                if self.isBetween(coord, prev[4], prev[6]) and self.isBetween(prev[5], prev[1], prev[1]+prevDist):
                    return True
                prev.pop()
            return False
    

Log in to reply
 

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