#335

Self Crossing

international master · 1925 · lc hard +32 · verified · 34.3% accepted · 417 likes · top 12%

play →

Description

You are given an array of integers distance.

You start at point (0, 0) on an X-Y plane, move distance[0] meters to the north, distance[1] meters to the west, distance[2] meters to the south, distance[3] meters to the east, and so on, rotating counter-clockwise after each move.

Return true if your path crosses itself or false if it does not.

Example 1:

Input: distance = [2,1,1,2]
Output: true
Explanation: The path crosses itself at the point (0, 1).

Example 2:

Input: distance = [1,2,3,4]
Output: false
Explanation: The path does not cross itself at any point.

Example 3:

Input: distance = [1,1,1,2,1]
Output: true
Explanation: The path crosses itself at the point (0, 0).

Code

1
2
3