Leetcode 335 - Self crossing

Note:

  • There are three situations
    • img
    • img
    • img
  • Note: For this situation, we just need to rotate it, and it becomes situation1.
    img

Question:

You are given an array of integers distance.

You start at point (0,0) on an X-Y plane and you move distance[0] meters to the north, then distance[1] meters to the west, distance[2] meters to the south, distance[3] meters to the east, and so on. In other words, after each move, your direction changes counter-clockwise.

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

Example:

img

1
2
Input: distance = [2,1,1,2]
Output: true

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @param {number[]} distance
* @return {boolean}
*/
var isSelfCrossing = function(distance) {
const len = distance.length;
if(len < 4) return false;
for (let i = 3; i < len; i++) {
if (distance[i] >= distance[i-2] && distance[i-1] <= distance[i-3]) return true;
if (i >= 4 && distance[i-2] <= distance[i] + distance[i-4] && distance[i-3] === distance[i-1]) return true;
if (i >= 5 && distance[i-1] <= distance[i-3] && distance[i] + distance[i-4] >= distance[i-2] && distance[i-2] > distance[i-4] && distance[i-1] + distance[i-5] >= distance[i-3]) return true;
}
return false;
};