Self Crossing
From LeetCode. Self-intersecting polygon. Solve the geometry problem "Self Crossing".
Examples
Input: [1,2,3]
Output: 0
Input: [2,3,4]
Output: 0
Hints
Path spirals counter-clockwise (NWSE repeat). Crossing only possible with edges 3, 4, or 5 steps ahead — no other pairs intersect in a simple spiral.
Three conditions for edge i: (1) d[i]≥d[i-2] and d[i-1]≤d[i-3] (crosses i+3). (2) d[i-1]==d[i-3] and d[i]+d[i-4]≥d[i-2] (crosses i+4). (3) d[i-2]≥d[i-4], d[i-1]≤d[i-3], d[i]≥d[i-2]-d[i-4], d[i-1]+d[i-5]≥d[i-3] (crosses i+5).
Start loop at i=3. Only last 6 values needed — O(1) space. All index subtractions safe since earlier indices exist by i≥3.
Self Crossing
**From LeetCode.** Self-intersecting polygon. Solve the geometry problem "Self Crossing".