335. Self Crossing

时间:2023-03-09 16:38:35
335. Self Crossing
    /*
* 335. Self Crossing
* 2016-7-10 by Mingyang
*/
// Categorize the self-crossing scenarios, there are 3 of them:
// 1. Fourth line crosses first line and works for fifth line crosses second line and so on...
// 2. Fifth line meets first line and works for the lines after
// 3. Sixth line crosses first line and works for the lines after
public boolean isSelfCrossing(int[] x) {
int l = x.length;
if(l <= 3) return false; for(int i = 3; i < l; i++){
if(x[i] >= x[i-2] && x[i-1] <= x[i-3]) return true; //Fourth line crosses first line and onward
if(i >=4)
{
if(x[i-1] == x[i-3] && x[i] + x[i-4] >= x[i-2]) return true; // Fifth line meets first line and onward
}
if(i >=5)
{
if(x[i-2] - x[i-4] >= 0 && x[i] >= x[i-2] - x[i-4] && x[i-1] >= x[i-3] - x[i-5] && x[i-1] <= x[i-3]) return true; // Sixth line crosses first line and onward
}
}
return false;
}