335. Self Crossing - cocoder39/coco39_LC GitHub Wiki
case 1: 4th line crosses 1st line, so as to 5th crosses 2nd, so on so forth.
┌───┐
│ │
└───┼──>
│
case 2: 5th line is incident to 1st line, so as to 6th line is incident to 2nd line, so on so forth.
┌───┐|
│ │|
└───┼
│
case 3: 6th line crosses 1st line, ...
┌──2───┐
│ │ ------6
│3 1| |
│ |5
└─────4──────
take case 3 as an example, move 5 to left or right, get first pair of conditions x[i - 2] >= x[i - 4] && x[i] + x[i - 4] >= x[i - 2]
; move 6 up and down, get second pair of conditions x[i - 1] <= x[i - 3] && x[i - 1] + x[i - 5] >= x[i - 3]
bool isSelfCrossing(vector<int>& x) {
for (int i = 3; i < x.size(); i++) {//4th and 1st
if (x[i] >= x[i - 2] && x[i - 1] <= x[i - 3])
return true;
if (i >= 4) {//5th and 1st
if (x[i - 1] == x[i - 3] && x[i] + x[i - 4] >= x[i - 2])
return true;
}
if (i >= 5) {//6th and 1st
if(x[i - 2] >= x[i - 4] && x[i] + x[i - 4] >= x[i - 2] && x[i - 1] <= x[i - 3] && x[i - 1] + x[i - 5] >= x[i - 3])
return true;
}
}
return false;
}