You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
708 B
29 lines
708 B
2 years ago
|
package class42;
|
||
|
|
||
|
public class Problem_0335_SelfCrossing {
|
||
|
|
||
|
public static boolean isSelfCrossing(int[] x) {
|
||
|
if (x == null || x.length < 4) {
|
||
|
return false;
|
||
|
}
|
||
|
if ((x.length > 3 && x[2] <= x[0] && x[3] >= x[1])
|
||
|
|| (x.length > 4
|
||
|
&& ((x[3] <= x[1] && x[4] >= x[2]) || (x[3] == x[1] && x[0] + x[4] >= x[2])))) {
|
||
|
return true;
|
||
|
}
|
||
|
for (int i = 5; i < x.length; i++) {
|
||
|
if (x[i - 1] <= x[i - 3] && ((x[i] >= x[i - 2])
|
||
|
|| (x[i - 2] >= x[i - 4] && x[i - 5] + x[i - 1] >= x[i - 3] && x[i - 4] + x[i] >= x[i - 2]))) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
int[] arr = { 2, 2, 3, 2, 2 };
|
||
|
System.out.println(isSelfCrossing(arr));
|
||
|
}
|
||
|
|
||
|
}
|