Skip to content

Create 731. My Calend #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions 731. My Calend731. My Calendar IIar II
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class MyCalendarTwo {
// map of start->end, each entry represents an interval [start, end)
// singleBooked is a superset of double booked, we don't have to worry about the double booked parts of it because we'll early return anyway.
std::map<int, int> singleBooked, doubleBooked;
public:

MyCalendarTwo() {}

bool book(int start, int end) {
{ // if [start, end) overlaps any double booked interval, we cannot insert.
const auto after = doubleBooked.lower_bound(end);
if(after != doubleBooked.begin()) {
const auto iter = std::prev(after);
if(iter->second > start) return false;
}
}

// find all intervals that overlap with this one.
// for each of those intervals, add the intersection of it and the current one to the doubleBooked interval set.
auto iter = singleBooked.upper_bound(start);
if(iter != singleBooked.begin() && (std::prev(iter)->second > start)) --iter;
const auto startIter = iter;
while(iter != singleBooked.end() && iter->first < end) {
doubleBooked[std::max(start, iter->first)] = std::min(end, iter->second);
++iter;
}
const auto endIter = iter;

// merge all overlapping intervals in singleBooked into one big interval.
int newStart = start;
int newEnd = end;
if(startIter != singleBooked.end()) newStart = min(start, startIter->first);
if(endIter != singleBooked.begin()) newEnd = max(newEnd, std::prev(endIter)->second);
const auto afterErase = singleBooked.erase(startIter, endIter);
singleBooked.emplace_hint(afterErase, newStart, newEnd);
return true;
}
};
Loading