Determine whether there is an intersection between a period of time and a bunch of periods of time. If the start and end times are the same, it is judged that there is an intersection
such as
判断
12:30:00--14:20:00
与下列时间段是否有交集
10:00:00-12:00:00, 12:10:00-12:50:00 , 14:30:00-15:00:00
Convert to timestamp and then compare them one by one in a loop
function is_cross($st1, $et1, $st2, $et2)
{
$status = $st2 - $st1;
if ($status > 0) {
$status2 = $st2 - $et1;
if ($status2 >= 0) {
return false;
} else {
return true;
}
} else {
$status2 = $et2 - $st1;
if ($status2 > 0) {
return true;
} else {
return false;
}
}
}
This can solve the problem, but we are looking for a better method with the smallest time complexity
public function inter(){
Convert the time into a timestamp, and then ------- compare.
If this
a bunch of time
needs to be used multiple times: you can use a line segment tree. "One-to-one comparison" will not be slower when used once.Python version, js should be the same
In this way, you can select the time period that overlaps with period from periods.
Convert the time into an integer 123000, and judge in reverse
a -- b
c -- d
Under what circumstances do these two time periods not overlap?