Drill

ProblemsC++ › events

Is there enough breathing room between slots

hardeventsIntervalsSortingC++

A scheduler requires a minimum pause between consecutive bookings. Verify every gap meets the requirement.

minimumGap(slots: list<Slot>, gapMinutes: int) → bool

C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

bool minimumGap(std::vector<Slot> slots, int gapMinutes) {
    
}

Worked examples

CallResult
minimumGap(std::vector<Slot>{Slot{1, 5}, Slot{10, 15}}, 5)true
minimumGap(std::vector<Slot>{Slot{1, 5}, Slot{8, 12}}, 5)false
minimumGap(std::vector<Slot>{Slot{1, 5}, Slot{15, 20}, Slot{25, 30}}, 5)true
minimumGap(std::vector<Slot>{Slot{1, 5}}, 10)true

Hint

Walk the list once, checking the gap between each pair of neighbours.

Reference solution in C++
bool minimumGap(std::vector<Slot> slots, int gapMinutes) {
    for (size_t i = 1; i < slots.size(); i++) {
        if (slots[i].start - slots[i - 1].end < gapMinutes) return false;
    }
    return true;
}

The same problem in another language

More events problems in C++