[leetcode]729. My Calendar I

[leetcode]729. My Calendar I


Analysis

waiting~—— [每天刷题并不难0.0]

Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.
Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)
For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
在这里插入图片描述

Implement

class MyCalendar {
public:
    MyCalendar() {
        
    }
    
    bool book(int start, int end) {
        for(auto b:books){
            if(max(b.first, start) < min(b.second, end))
                return false;
        }
        books.push_back({start, end});
        return true;
    }
private:
    vector<pair<int, int>> books;
};

/**
 * Your MyCalendar object will be instantiated and called as such:
 * MyCalendar obj = new MyCalendar();
 * bool param_1 = obj.book(start,end);
 */

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/84748187