Self Crossing

Hard

Topics
ArrayMathGeometry

You start at the origin and move counter-clockwise: nums[0] north, nums[1] west, nums[2] south, nums[3] east, and so on. Given the distance array nums, return true if the path crosses itself, otherwise false.

Example 1

20111223
Input:  nums = [2,1,1,2]
Output: true

Example 2

10213243
Input:  nums = [1,2,3,4]
Output: false

Example 3

10111213
Input:  nums = [1,1,1,1]
Output: true

Constraints

  • 1 <= nums.length <= 8
  • 1 <= nums[i] <= 8
Editorial — intuition, approach, complexity and pitfalls

Intuition

The path turns counter-clockwise every step, which means it is either spiralling outward forever — never crossing — or it has started to spiral inward, and then a crossing happens within a bounded number of steps. That is why a geometric case analysis beats simulating segments.

There are exactly three ways the current segment can touch an earlier one. It can cross the segment three back, which happens when the spiral stops growing. It can lie exactly on top of the segment four back, which needs the segment one back to equal the one three back. Or it can cross the segment six back, the case where the spiral tightens gradually enough to take two more turns before meeting.

Nothing further back is reachable: once the path has turned inward it is confined, so a segment can only ever meet one of those three.

Approach

  1. Walk i from 3 upward.
  2. Case 1: x[i] >= x[i-2] and x[i-1] <= x[i-3].
  3. Case 2 (needs i >= 4): x[i-1] == x[i-3] and x[i] + x[i-4] >= x[i-2].
  4. Case 3 (needs i >= 5): the segment two back has grown, and the current and four-back segments together span it, with the same condition one level out.
  5. Return true on the first hit, false if the walk completes.

Dry Run

x = [2,1,1,2]      north 2, west 1, south 1, east 2

  i = 3:  x[3] = 2 >= x[1] = 1   and   x[2] = 1 <= x[0] = 2
          case 1 fires -- the eastward segment crosses the northward one

answer true

  [1,2,3,4]       -> false   a strictly growing spiral never crosses
  [1,1,1,1]       -> true    case 1: 1 >= 1 and 1 <= 1
  [3,3,3,2,1,1]   -> false   tightening, but not yet far enough to meet

Complexity

  • Time — O(n): one pass with three constant-time tests per step.
  • Space — O(1). No coordinates are ever materialised, which is the point of the case analysis.
  • Building the actual segments and testing them pairwise is O(n²) and needs careful axis-aligned intersection code. It is a legitimate answer and far easier to get right; the three-case version is what the problem is actually asking for.

Common Pitfalls

  • Using > instead of >= in the comparisons. Touching counts as crossing, so [1,1,1,1] must return true.
  • Forgetting the i >= 4 and i >= 5 guards, which index before the start of the array.
  • Using elif where the cases should each be tested. They are mutually exclusive in practice, so chaining happens to work — but the independence is worth checking rather than assuming.
  • Deriving only the first two cases. The six-back case is the one people miss, and it is the reason naive solutions pass the samples and fail on longer spirals.
  • Trying to prove the three cases are exhaustive on the spot. They follow from the path being confined once it turns inward; knowing that argument matters more than rederiving the inequalities.
  • Simulating with a set of visited lattice points. The distances reach 10⁴ in the general version of this problem, so the point set is far too large — here the bound of 8 hides it.

Full Solution — all languages

class Solution:
    def isSelfCrossing(self, nums):
        x = nums
        n = len(x)
        for i in range(3, n):
            if x[i] >= x[i - 2] and x[i - 1] <= x[i - 3]:
                return True
            if i >= 4 and x[i - 1] == x[i - 3] and x[i] + x[i - 4] >= x[i - 2]:
                return True
            if i >= 5 and x[i - 2] >= x[i - 4] and x[i] + x[i - 4] >= x[i - 2] and x[i - 1] <= x[i - 3] and x[i - 1] + x[i - 5] >= x[i - 3]:
                return True
        return False
SensAI
Beta
Tap the mic to stop voice mode