I think the comparison logic can be simplified considerably. If the last hour of a period is numerically less than the first hour, it is to be interpreted as the corresponding hour on the next day. We can add the value 24 to the last hour in that case, so that the "simple" comparison for overlapping intervals gives the correct result:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
endPeriod1 += 24;
}
if (endPeriod2 < startPeriod2) {
endPeriod2 += 24;
}
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
No lists are needed for this implementation.
A better name for the function might be periodsOverlap
or something like that.
If endPeriod < startPeriod
is to be interpreted as two time intervals on the same day then it is still possible to compute the result without using lists, e.g. by recursive calls of the function until the "simple" case is reached:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
return timesCrossLate(startPeriod1, 23, startPeriod2, endPeriod2)
|| timesCrossLate(0, endPeriod1, startPeriod2, endPeriod2);
} else if (endPeriod2 < startPeriod2) {
return timesCrossLate(startPeriod1, endPeriod1, startPeriod2, 23)
|| timesCrossLate(startPeriod1, endPeriod1, 0, endPeriod2);
} else {
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
}
I think the comparison logic can be simplified considerably. If the last hour of a period is numerically less than the first hour, it is to be interpreted as the corresponding hour on the next day. We can add the value 24 to the last hour in that case, so that the "simple" comparison for overlapping intervals gives the correct result:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
endPeriod1 += 24;
}
if (endPeriod2 < startPeriod2) {
endPeriod2 += 24;
}
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
No lists are needed for this implementation.
A better name for the function might be periodsOverlap
or something like that.
I think the comparison logic can be simplified considerably. If the last hour of a period is numerically less than the first hour, it is to be interpreted as the corresponding hour on the next day. We can add the value 24 to the last hour in that case, so that the "simple" comparison for overlapping intervals gives the correct result:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
endPeriod1 += 24;
}
if (endPeriod2 < startPeriod2) {
endPeriod2 += 24;
}
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
No lists are needed for this implementation.
A better name for the function might be periodsOverlap
or something like that.
If endPeriod < startPeriod
is to be interpreted as two time intervals on the same day then it is still possible to compute the result without using lists, e.g. by recursive calls of the function until the "simple" case is reached:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
return timesCrossLate(startPeriod1, 23, startPeriod2, endPeriod2)
|| timesCrossLate(0, endPeriod1, startPeriod2, endPeriod2);
} else if (endPeriod2 < startPeriod2) {
return timesCrossLate(startPeriod1, endPeriod1, startPeriod2, 23)
|| timesCrossLate(startPeriod1, endPeriod1, 0, endPeriod2);
} else {
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
}
I think the comparison logic can be simplified considerably. If the last hour of a period is numerically less than the first hour, it is to be interpreted as the corresponding hour on the next day. If weWe can add the value 24 to the last hour in that case, so that the "simple" comparison for overlapping intervals gives the correct result:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
endPeriod1 += 24;
}
if (endPeriod2 < startPeriod2) {
endPeriod2 += 24;
}
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
No lists are needed for this implementation.
A better name for the function might be periodsOverlap
or something like that.
I think the comparison logic can be simplified considerably. If the last hour of a period is numerically less than the first hour, it is to be interpreted as the corresponding hour on the next day. If we add the value 24 to the last hour in that case, the "simple" comparison for overlapping intervals gives the correct result:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
endPeriod1 += 24;
}
if (endPeriod2 < startPeriod2) {
endPeriod2 += 24;
}
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
No lists are needed for this implementation.
I think the comparison logic can be simplified considerably. If the last hour of a period is numerically less than the first hour, it is to be interpreted as the corresponding hour on the next day. We can add the value 24 to the last hour in that case, so that the "simple" comparison for overlapping intervals gives the correct result:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
endPeriod1 += 24;
}
if (endPeriod2 < startPeriod2) {
endPeriod2 += 24;
}
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
No lists are needed for this implementation.
A better name for the function might be periodsOverlap
or something like that.
I think the comparison logic can be simplified considerably. If the last hour of a period is numerically less than the first hour, it is to be interpreted as the corresponding hour on the next day. If we add the value 24 to the last hour in that case, the "simple" comparison for overlapping intervals gives the correct result:
static boolean timesCrossLate(int startPeriod1, int endPeriod1, int startPeriod2, int endPeriod2) {
if (endPeriod1 < startPeriod1) {
endPeriod1 += 24;
}
if (endPeriod2 < startPeriod2) {
endPeriod2 += 24;
}
return startPeriod1 < endPeriod2 && endPeriod1 > startPeriod2;
}
No lists are needed for this implementation.