zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    Thawte SSL Web Server 多域型SSL证书
    易维信(EVTrust)支招五大技巧识别钓鱼网站
    Thawte SSL Web Server
    Thawte 企业版代码签名证书
    python数据分析(四)
    python数据分析(三)
    python数据分析(二)
    python数据分析(一)
    MSQL基础知识
    c#中调用c++程序
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10059705.html
Copyright © 2011-2022 走看看