Leetcode Problem:
Summary
- Implementing a calendar system to avoid triple booking.
Approach
- This solution uses a data structure to store the events in the calendar
- It first checks for overlapping events with existing events in the calendar and returns false if a triple booking is found
- If no overlap is found, it adds the new event to the calendar.
Complexity
- O(n log n) due to sorting of events in the calendar
Explain
- The MyCalendarTwo class uses a vector to store the events in the calendar
- The book method first checks for overlapping events with existing events in the calendar
- It uses a nested loop to check for overlapping events and returns false if a triple booking is found
- If no overlap is found, it adds the new event to the calendar
- The time complexity of this solution is O(n log n) due to sorting of events in the calendar.
Solution Code:
struct _pair{
int start;
int end;
};
class MyCalendarTwo {
public:
vector<_pair> bookList;
MyCalendarTwo() {
}
bool book(int start, int end) {
vector<_pair> doubleBookList;
for(_pair book : bookList){
if(book.start >= end || book.end <= start){
continue;
}
int s = max(book.start, start);
int e = min(book.end, end);
for(_pair db: doubleBookList){
if(db.start >= e || db.end <= s){
continue;
}
return false;
}
doubleBookList.push_back({s, e});
}
bookList.push_back({start, end});
return true;
}
};
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo* obj = new MyCalendarTwo();
* bool param_1 = obj->book(start,end);
*/
'알고리즘' 카테고리의 다른 글
All O`one Data Structure (0) | 2025.02.13 |
---|---|
Design Circular Deque (0) | 2025.02.13 |
My Calendar I (0) | 2025.02.13 |
Sum of Prefix Scores of Strings (0) | 2025.02.13 |
Max Sum of a Pair With Equal Sum of Digits (0) | 2025.02.12 |