search

Home  >  Q&A  >  body text

javascript - Find the optimal solution for the intersection between time periods

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

PHP中文网PHP中文网2700 days ago1193

reply all(7)I'll reply

  • 世界只因有你

    世界只因有你2017-06-30 09:55:32

    public function inter(){

        $tar=[6,4];
        if($tar[0]>$tar[1]){
            $temp=$tar[0];
            $tar[0]=$tar[1];
            $tar[1]=$temp;
        }
        $all=[
            [5,6],
            [7,9],
            [1,4],
            [3,1],
            [1,3],
            [8,7]
        ];
        //排序
        foreach ($all as &$v){
            if($v[0]>$v[1]){
                $temp=$v[0];
                $v[0]=$v[1];
                $v[1]=$temp;
            }
        }
        foreach ($all as $k=>$v){
            $left=$tar[0]>$v[1];
            $right=$v[0]>$tar[1];
            if(!($left||$right)){
                var_dump($v);
            }
        }
    
    }

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-30 09:55:32

    Convert the time into a timestamp, and then ------- compare.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-30 09:55:32

    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.

    reply
    0
  • 迷茫

    迷茫2017-06-30 09:55:32

    # -*- coding: utf-8 -*-
    
    def is_mixed(t1, t2):
        '''
        假定时间段格式是:"10:00:00-12:00:00"
        判断 t1,t2是否有交集
        '''
        s1, e1 = t1.split("-")
        s2, e2 = t2.split("-")
    
        if s1 > e2 and s2 > e1:
            return True
        if s2 > e1 and s1 > e2:
            return True
    
        return False
    
    t1 = "12:30:00-04:20:00"
    t2 = "03:00:00-22:23:00"
    
    if is_mixed(t1, t2):
        print "有交集!"
    else:
        print "木有交集!"
    

    Python version, js should be the same

    reply
    0
  • ringa_lee

    ringa_lee2017-06-30 09:55:32

    转换成时间戳能方便比较点

    reply
    0
  • 怪我咯

    怪我咯2017-06-30 09:55:32

    In this way, you can select the time period that overlaps with period from periods.

    period  = "12:30:00-14:20:00"
    periods  = ["10:00:00-12:00:00", "12:10:00-12:50:00","14:30:00-15:00:00"]
    
    print([x+'-'+y for [b,e] in [period.split("-")] for [x,y] in [p.split("-") for p in periods] if x<=e and y>=b])

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-30 09:55:32

    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?

    1. b < c

    2. a > d

    3. reply
      0
  • Cancelreply