Self Crossing

Three geometric cases — the path crosses iff one of "4th edge crosses 1st", "5th meets 1st", or "6th crosses 1st" holds.

Hard
Time O(n) Space O(1)
LeetCode
3 min read
geometry math

Problem#

A path moves north, west, south, east repeatedly with distances distance[i]. Return true iff the path self-crosses.

Examples#

  • [2,1,1,2]true.
  • [1,2,3,4]false.

Constraints#

  • 1 <= distance.length <= 10^5.

Approach#

It can be proved that any crossing falls into exactly one of three local cases:

  1. Edge i crosses edge i-3 (the spiral closes inward).
  2. Edge i meets edge i-4 head-on.
  3. Edge i crosses edge i-5 (the spiral widens then narrows).

Each case is a small inequality on three to four consecutive distances.

Solution#

Self Crossing
class Solution {
public:
bool isSelfCrossing(vector<int>& d) {
int n = d.size();
for (int i = 3; i < n; ++i) {
if (d[i] >= d[i-2] && d[i-1] <= d[i-3]) return true;
if (i >= 4 && d[i-1] == d[i-3] && d[i] + d[i-4] >= d[i-2]) return true;
if (i >= 5
&& d[i-2] >= d[i-4]
&& d[i-3] >= d[i-1]
&& d[i-1] + d[i-5] >= d[i-3]
&& d[i] + d[i-4] >= d[i-2]) return true;
}
return false;
}
};

Editorial#

The hardest part is the proof that only these three local cases can produce a crossing — beyond i-5, the path is too far away. Once accepted, each case is a constant-time inequality, giving an O(n) walk.

Complexity#

  • Time: O(n).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.