Python solution


  • 0
    C
    class Solution(object):
        def isSelfCrossing(self, x):
            """
            :type x: List[int]
            :rtype: bool
            """
            last = [[0,0], [0,0]] # v1, v2, h1, h2 for last 4 length
            idx = 0 # 0 for horizontal, 1 for vertical
            is_outer = True # inward spiral or outward spiral
            for item in x:
                if is_outer:
                    if item > last[idx][1]:
                        last[idx][0] = last[idx][1]
                    else:
                        is_outer = False
                        if last[idx][0]+ item >= last[idx][1]:
                            last[1-idx][1] -= last[1-idx][0]
                else:
                    if item >= last[idx][1]: return True
                last[idx][1] = item
                idx = 1 - idx
            return False

Log in to reply
 

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