【问题标题】:Any better solution for this scheduling algorithm?这种调度算法有更好的解决方案吗?
【发布时间】:2014-02-26 09:51:02
【问题描述】:

你有 N 个人。每个人都有一份空闲时间列表。

例如,人 1 的空闲时间段可能是 [(9, 9.5); (11, 12.5)] 这意味着他在 9 点到 9:30 之间以及 11:00 到 12:30 之间有空。

你想找一个时间段,这样你就可以把所有 N 个人聚集在一起,开一个 2 小时的会议。

写一个方法:

输入:

一个list的list,里面的每个list都是一个空闲时间段的list 人

输出:

一个或多个可用于召开 N 人会议的时间段 持续2个小时;或者你找不到这样的时间段


我的想法是这样的:

  • 按如下方式合并两个人的时间段列表:如果两个时间段不重叠,则删除开始时间早的那个;否则,将重叠部分(新时间段)放入新列表中,并删除开始时间较早的部分。继续合并。

  • 合并完成后,我们得到一个包含两个人时间重叠的新列表,然后将其与第三个人合并,以此类推,直到所有列表合并。

  • 扫描最终的合并列表,找出 2 小时的时段。

如果假设每个人都有M 空闲时间段,则此算法为**O(N*M)。


有没有更好的解决方案?

【问题讨论】:

    标签: algorithm


    【解决方案1】:

    您的输入大小为 N*M,因此您无法获得更好的时间复杂度 - 您必须读取所有输入。


    最初我误读了问题并在下面发布了算法;它的时间复杂度仅略大于输入大小 (O(nm log nm)),但可以解决更一般的情况,即如果您不能同时开会,则希望最大化会议中的人数。

    1. Merge periods (a, b), (b, c) into (a, c)
    2. Forget periods shorter than 2 hours
    3. Transform your input into list of 
        (timestamp_of_beginning, person_id, True)
        (timestamp_of_ending - 2 hours, person_id, False)
    4. Sort the list by first element of tuple
    5. Iterate over the list, with a set A, 
       adding person_id to A on (_, person_id, True),
       and removing on (_, person_id, False). 
    
       If after processing some item (t, _, _) 
       A has k elements, you can have a meeting of k persons, their ids are in A.
    
    6. The maximal size of A during iteration is the maximal number of persons 
       that can have the meeting.
    

    【讨论】:

    • 时间复杂度是多少?
    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2010-11-08
    相关资源
    最近更新 更多