leetcode 732. 我的日程安排表 III
题目:732. 我的日程安排表 III - 力扣(LeetCode)
这个数据规模,暴力就够了
struct Book {
int begin;
int end;
Book(int b, int e) {
begin = b;
end = e;
}
};
class MyCalendarThree {
public:
MyCalendarThree() {
}
int book(int startTime, int endTime) {
Book* b = new Book(startTime, endTime);
books.push_back(b);
for (size_t i = books.size() - 1; i > 0; i--) {
if (books[i]->begin >= books[i - 1]->begin) {
break;
}
Book* t = books[i];
books[i] = books[i - 1];
books[i - 1] = t;
}
int ret = 0;
int current = 0;
ends.clear();
for (size_t i = 0; i < books.size(); i++) {
current++;
while (ends.size() && ends[ends.size() - 1] <= books[i]->begin) {
current--;
ends.pop_back();
}
if (current > ret) {
ret = current;
}
ends.push_back(books[i]->end);
for (size_t k = ends.size() - 1; k > 0; k--) {
if (ends[k] <= ends[k - 1]) {
break;
}
int t = ends[k];
ends[k] = ends[k - 1];
ends[k - 1] = t;
}
}
return ret;
}
private:
vector<Book*> books;
vector<int> ends;
};