/335. Self Crossing

335. Self Crossing

Hard
Arrays34.4% acceptance

Given an integer array move_lengths, simulate a path starting at (0, 0) on a 2D grid. The path moves move_lengths[0] units north, move_lengths[1] units west, move_lengths[2] units south, move_lengths[3] units east, and continues in this counter-clockwise pattern. Return True if the path crosses itself at any point, otherwise return False.

Example 1

Input: [3,2,2,3]

Output: True

Explanation: The path crosses itself after the fourth move.

Example 2

Input: [4,3,2,1]

Output: False

Explanation: The path does not cross itself.

Example 3

Input: [2,2,2,2,1]

Output: True

Explanation: The path crosses itself after the fifth move.

Constraints

  • 1 <= len(move_lengths) <= 105
  • 1 <= move_lengths[i] <= 105 for all valid i
Python (current runtime)

Case 1

Input: [5,3,3,5]

Expected: True

Case 2

Input: [1,2,3,4,5,6]

Expected: False

Case 3

Input: [2,2,2,2]

Expected: True

Case 4

Input: [10,1,1,10,1,1]

Expected: True

Case 5

Input: [7,6,5,4,3,2,1]

Expected: False