首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查3个相同房间的单个会议室时间表可用性的方法或算法?

检查3个相同房间的单个会议室时间表可用性的方法或算法?
EN

Stack Overflow用户
提问于 2018-07-25 10:09:32
回答 1查看 354关注 0票数 2

如果我想提供一个类似日历的时间表,带有时间间隔单元30分钟,用于预订会议室。

一个单独的时间表来表示3个实际会议室的可用性。假设这个预订会议室名为A,3个实际房间是A1、A2、A3。

每个人只能预定固定的3个小时,有什么更好的方法来检查和表示“当前会议室的可用性”?

例如。

有人预订了01:00 ~ 04:00的A1

有人预订了A2,从02:00到05:00

有人预订了3:00~6:00的A3

所以00:00 ~ 03:00可以预约,05:00 ~ 08:00也可以预约,但01:00~ 04:00不可以。

如何用精确的可用时间间隔检查会议室时间表A?

EN

回答 1

Stack Overflow用户

发布于 2018-07-25 18:53:42

您要问的是一个相当常见的问题-合并间隔以定位空闲/可用插槽。

我可以提供一个demo给你用LocalDate解决java8中的这种interval merging问题,你可以根据需要用LocalTime替换它。

此外,由于您有多个房间,您需要稍微修改间隔以融入其中。

演示和测试案例:

代码语言:javascript
复制
public class Extra_1_interval_merge {
    @Test
    public void testAvailablePeriod() {
        List<MyPeriod> absentPeriods0 = new ArrayList<>();
        absentPeriods0.add(makePeriod(LocalDate.now(), LocalDate.now().plusDays(1)));
        absentPeriods0.add(makePeriod(LocalDate.now().plusDays(4), LocalDate.now().plusDays(6)));
        absentPeriods0.add(makePeriod(LocalDate.now().plusDays(2), LocalDate.now().plusDays(3)));


        List<MyPeriod> absentPeriods1 = new ArrayList<>();
        absentPeriods1.add(makePeriod(LocalDate.now(), LocalDate.now().plusDays(2)));
        absentPeriods1.add(makePeriod(LocalDate.now().plusDays(5), LocalDate.now().plusDays(7)));

        List<List<MyPeriod>> absentListList = new ArrayList<>();
        absentListList.add(absentPeriods0);
        absentListList.add(absentPeriods1);
        System.out.println(getAvailablePeriods(absentListList));
    }

    private List<MyPeriod> getAvailablePeriods(List<List<MyPeriod>> absentListList) {
        // Step - 1: Collect all periods;
        List<MyPeriod> tempList = new ArrayList<>();
        absentListList.stream().forEach(list -> tempList.addAll(list));

        // Step - 2: Sort the periods based on the startDate and then endDate;
        List<MyPeriod> absentList = tempList.stream().sorted((period1, period2) -> {
            if (!period1.startDate.isEqual(period2.startDate)) {
                return period1.startDate.compareTo(period2.startDate);
            } else {
                return period1.endDate.compareTo(period2.endDate);
            }
        }).collect(toList());

        // Step - 3: Merge all overlapped periods to form an one-dimension occupied period list;
        List<MyPeriod> mergedPeriods = new ArrayList<>();
        for (MyPeriod period : absentList) {
            if (mergedPeriods.isEmpty()) {
                mergedPeriods.add(period);
            } else {
                MyPeriod lastPeriod = mergedPeriods.get(mergedPeriods.size() - 1);
                if (!lastPeriod.endDate.isBefore(period.startDate)) {
                    if (lastPeriod.endDate.isBefore(period.endDate)) {
                        lastPeriod.endDate = period.endDate;
                    }
                } else {
                    mergedPeriods.add(period);
                }
            }
        }

        // Step - 4: Pick the periods from the occupied period list;
        List<MyPeriod> availablePeriods = new ArrayList<>();
        for (int i = 0, len = mergedPeriods.size(); i < len - 1; i++) {
            availablePeriods.add(makePeriod(mergedPeriods.get(i).endDate, mergedPeriods.get(i+1).startDate));
        }
        return availablePeriods;
    }

    private MyPeriod makePeriod(LocalDate startDate, LocalDate endDate) {
        MyPeriod thePeriod = new MyPeriod();
        thePeriod.startDate = startDate;
        thePeriod.endDate = endDate;
        return thePeriod;
    }

    class MyPeriod {
        LocalDate startDate;
        LocalDate endDate;
        @Override
        public String toString() {
            return String.format("Start: %s, End: %s", startDate, endDate);
        }
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51509881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档