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)
  • 相关阅读:
    Linux运维之监控CPU和内存的日志工具
    Linux磁盘缓存的有趣实验
    Linux运维之内存分析2
    Linux运维之内存分析
    使用kubectl create 和 kubectl apply创建资源对象的区别
    Docker学习:Image的本地存储结构
    Docker 空间使用分析与清理
    HeidiSQL、Navicat、mysql命令和source命令导入sql脚本的速度比较
    Centos 7.2天兔(Lepus 3.8)数据库监控系统部署
    MegaCli 监控raid状态
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10059705.html
Copyright © 2011-2022 走看看