• 731. My Calendar II


    Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking.

    Your class will have one 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.

    triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.)

    For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a triple 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)

    Example 1:

    MyCalendar();
    MyCalendar.book(10, 20); // returns true
    MyCalendar.book(50, 60); // returns true
    MyCalendar.book(10, 40); // returns true
    MyCalendar.book(5, 15); // returns false
    MyCalendar.book(5, 10); // returns true
    MyCalendar.book(25, 55); // returns true
    Explanation: 
    The first two events can be booked.  The third event can be double booked.
    The fourth event (5, 15) can't be booked, because it would result in a triple booking.
    The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked.
    The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event;
    the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
    

    Note:

    • The number of calls to MyCalendar.book per test case will be at most 1000.
    • In calls to MyCalendar.book(start, end)start and end are integers in the range [0, 10^9].
     

    Approach #1: C++. [Brute Force]

    class MyCalendarTwo {
    public:
        MyCalendarTwo() {
            
        }
        
        bool book(int start, int end) {
            for (auto it : overlap) {
                if (max(start, it.first) <= min(end, it.second)) return false;
            }
            
            for (auto it : booked) {
                int ss = max(start, it.first);
                int ee = min(end, it.second);
                
                if (ss <= ee) overlap.push_back({ss, ee});
            }
            
            booked.push_back({start, end});
            return true;
        }
        
    private:
        vector<pair<int, int>> overlap;
        vector<pair<int, int>> booked;
    };
    

      

    Approach #2: C++. [map].

    class MyCalendarTwo {
    public:
        MyCalendarTwo() {
            
        }
        
        bool book(int start, int end) {
            ++books[start];
            --books[end];
            
            int count = 0;
            for (auto it : books) {
                count += it.second;
                if (count >= 3) {
                    --books[start];
                    ++books[end];
                    return false;
                }
                
                if (it.first > end) break;
            }
            
            return true;
        }
        
    private:
        map<int, int> books;
    };
    

      

    In this solution we use map{KEY} to store the time point, and using map{VALUE} to store the booking times point. In every time we travel the map inorder and count the number of booking times in this time ranges. if count >= 3 return false;

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    CI框架用cookie实现用户自动登录
    php预定义常量
    ubuntu root密码
    jQuery的form中ajaxSubmit参数
    优酷视屏播放去广告链接id_XXX.html内容替换红色
    linux 重启apache:apachectl -k graceful
    python深copy与浅copy的区别
    python3中pathlib库的Path类方法汇总
    unittest使用discover加载不同目录下的case失败,提示Path must be within the project
    自定义HTMLTestRunner报告case名称
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10059705.html
Copyright © 2020-2023  润新知