// Categorize the selfcrossing 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 class Solution {
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[i2] && x[i1] <= x[i3]) return true; //Fourth line crosses first line and onward
if(i >=4)
{
if(x[i1] == x[i3] && x[i] + x[i4] >= x[i2]) return true; // Fifth line meets first line and onward
}
if(i >=5)
{
if(x[i2]  x[i4] >= 0 && x[i] >= x[i2]  x[i4] && x[i1] >= x[i3]  x[i5] && x[i1] <= x[i3]) return true; // Sixth line crosses first line and onward
}
}
return false;
}
}
Java Oms with explanation


Best solution so far. However, I think it would be a little bit more helpful if you could rephrase your comments somehow.
 The first
if
checks if current line crosses the line 3 steps ahead of it  The second
if
checks if current line crosses the line 4 steps ahead of it  The third
if
checks if current line crosses the line 6 steps ahead of it
If none of the above condition is satisfied, there must not be any cross.
Other minor comment:
if(l <= 3) return false;
is not necessary
 The first

I really like this solution, and really brilliant solution! I go ahead and consolidated it a little bit with new comments:
public boolean isSelfCrossing(int[] x) { for(int i=3, l=x.length; i<l; i++) { if(x[i]>=x[i2] && x[i1]<=x[i3]) return true; // Case 1: current line crosses the line 3 steps ahead of it else if(i>=4 && x[i1]==x[i3] && x[i]+x[i4]>=x[i2]) return true; // Case 2: current line crosses the line 4 steps ahead of it else if(i>=5 && x[i2]>=x[i4] && x[i]+x[i4]>=x[i2] && x[i1]<=x[i3] && x[i1]+x[i5]>=x[i3]) return true; // Case 3: current line crosses the line 6 steps ahead of it } return false; }
Here is an image that illustrates the three possible crosses:

Nice answer, I registered on this website 7 months ago and I still write redundant codes here and there.
Sorry I didn't respond in time.
Actually I didn't come up with all the possible cross scenarios in the first place, I learned them from all the test cases.
Hope to exchange ideas in the future!

Base on your solution and comments, here is the images.
So far the best solution.class Solution { public: bool isSelfCrossing(vector<int>& x) { for(int i=3, l=x.size(); i<l; i++) { // Case 1: current line crosses the line 3 steps ahead of it if(x[i]>=x[i2] && x[i1]<=x[i3]) return true; // Case 2: current line crosses the line 4 steps ahead of it else if(i>=4 && x[i1]==x[i3] && x[i]+x[i4]>=x[i2]) return true; // Case 3: current line crosses the line 6 steps ahead of it else if(i>=5 && x[i2]>=x[i4] && x[i]+x[i4]>=x[i2] && x[i1]<=x[i3] && x[i1]+x[i5]>=x[i3]) return true; } return false; } /* i2 case 1 : i1┌─┐ └─┼─>i i3 i2 case 2 : i1 ┌────┐ └─══>┘i3 i i4 (i overlapped i4) case 3 : i4 ┌──┐ │i<┼─┐ i3│ i5│i1 └────┘ i2 */ };

Pretty awesome solution for a problem that tends to produce ugly code and lots of branches. One minor note: case two really makes sense only when
i == 4
. Fori >= 5
it becomes redundant because when a line merges with the (i − 4)th line, it also crosses the (i − 5)th line right where the (i − 4)th line begins. This can allow us to reduce the number of branches in the loop, like this:public boolean isSelfCrossing(int[] x) { if (x.length >= 5 && x[1] == x[3] && x[2]  x[4] <= x[0]) { // 5th line merges with the 1st one return true; } for (int i = 3; i < x.length; ++i) { if (x[i  1] <= x[i  3] && x[i] >= x[i  2] // crosses the line three steps behind  i >= 5 && x[i  2] >= x[i  4] && x[i  3]  x[i  1] >= 0 && x[i  3]  x[i  1] <= x[i  5] && x[i] >= x[i  2]  x[i  4]) { // crosses the line five steps behind return true; } } return false; }