java - 表示和比较时隙

标签 java algorithm

我需要存储餐厅订位的时间段,然后看看有没有碰撞...

For example - Total tables - 4 
1) 9 - 11 , 3 tables   
2) 9 - 10 , 1 tables (Need to do search if any table left 
                       with constraint to above booking)

我如何存储时间段和表格并与其他人进行比较...

我应该使用什么数据结构...

如果我使用 HashMap 什么可以是键和值,

我已经设计了所有其他类和方法,但无法找到解决时隙冲突问题的方法

collision example - 

total - 4 tables

1) 9-10 , 3 tables

2) 9-11 , 1 table

3) 9-12 , 2 tables  (collision , table not available)

最佳答案

您可以通过将可用时间分成 15 分钟的 block (或适合您的任何其他 block 大小)来简化问题。对于餐厅预订,我敢打赌 15 分钟的时间段没问题。

然后你可以有一个简单的 int[] 来存储每个时间段的预订表数。

例子: 您的餐厅从上午 9 点营业到晚上 9 点,因此营业时间为 12 小时,每个时段有 4 个时段。所以你需要一个有 48 个槽的 int[]。现在,当您预订 9 点到 11 点的 3 个 table 时,您将前 8 个空位(即 9 点到 11 点)增加 3。第二次预订会将前 4 个空位增加 1。如果预订会使您的其中一个空位增加到超过可用赌 table 限制,您知道您需要拒绝它。

final int MAX_TABLES = 4;
final int OPENING= 9;
final int CLOSING= 21;
final int SLOTS= 4;
int[] booking = new int[(CLOSING - OPENING) * SLOTS];

public void main() {
    // no tables booked
    Arrays.fill(booking,0);

    doBooking(3, 0, 8);
    doBooking(1, 4, 8);
    doBooking(1, 4, 12);
}

public void doBooking(int tables, int startSlot, int endSlot) {
    for (int slot= startSlot, slot < endSlot, slot++) {
        if (booking[slot] + tables > MAX_TABLES) {
            throw new Exception("no free table at slot "+slot);
        }
    }
    for (int slot= startSlot, slot < endSlot, slot++) {
        booking[slot] += tables;
    }
}

这应该会给您灵感。还有事情要做,例如正确的异常处理、从时间到槽的转换等。另请注意,这可能是不正确的 Java 代码,因为我没有测试它,也没有在 GUI 中编写它。

关于java - 表示和比较时隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27046368/

相关文章:

algorithm - 双指数问题?

java - 并行日志聚合的最佳方法

java - 实时 Excel 更新

java - 在 Java 中使用多个分隔符分割字符串时出现不需要的元素

algorithm - 确定这些不同循环的大 O 运行时间?

arrays - 子数组长度 > 0 的最小和

c# - 图表轴的步长计算

java - 为什么它不是我当前的位置,只有一个位置 37.4219983,-122.084?

java - 为两个完全不同的类创建一个比较器是一种不好的做法吗?

java - 如何使用 AJAX 更新 Wicket DataView?