首页 > 其他分享 >[Google] LeetCode 729 My Calendar I

[Google] LeetCode 729 My Calendar I

时间:2022-08-22 03:33:17浏览次数:71  
标签:Google end int start calendar Calendar My event MyCalendar

You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.

A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).

The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.

Implement the MyCalendar class:

  • MyCalendar() Initializes the calendar object.
  • boolean book(int start, int end) Returns 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.

Solution

我们用 \(vector\) 套 \(pair\) 来维护时间坐标。那么如何查找当前的时间间隔是否与之前的有重叠呢

一种办法就是朴素的暴力 \(O(N)\), 不妨二分位置,如果在二分的过程中出现了重叠,那么直接 \(false\) 即可;否则在二分出的位置中插入当前的区间

点击查看代码
class MyCalendar {
private:
    vector<pair<int,int>> t;
public:
    MyCalendar() {
        
    }
    
    bool book(int start, int end) {
        int n = t.size();
        int l = 0, r = n-1;
        int mid;
        while(l<=r){
            mid = l+(r-l)/2;
            int curs = t[mid].first, cure = t[mid].second;
            if(max(start, curs)<=min(end-1,cure))return false;
            else if(start<curs) r=mid-1;
            else l=mid+1;
        }
        t.insert(t.begin()+l, {start, end-1});
        return true;
    }
};

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

标签:Google,end,int,start,calendar,Calendar,My,event,MyCalendar
From: https://www.cnblogs.com/xinyu04/p/16611600.html

相关文章

  • mybaits-plus+druid 使用 LocalDateTime 出现nested exception is java.sql.SQLFeatur
    解决方案一(推荐)目前druid已经修复了这个问题并提交了新版本,最优直接选择升级druid至1.1.21或以上releases版本地址 https://github.com/alibaba/druid/releases/tag/......
  • mysql6/视图/触发器/事务/四种隔离级别/事务日志/mvcc/内置函数/存储过程/索引/索引的
    视图触发器事务事务处理四种隔离级别事务日志MVCC内置函数存储过程索引索引的意义慢查询优化查询索引模拟视图1.什么是视图?视图是类似于临时表,由sql......
  • 10.异步mysql
    python中操作mysql连接、操作、断开都是网络IO#安装支持异步aiomysql的模块pip3installaiomysqlasyncdefexecute():#网络IO操作,连接数据库,遇到IO切换任务......
  • mycat读写分离、mysql主从的安装
    数据库安装手册目录数据库安装手册1、数据库安装1.1环境准备1.1.1关闭selinux1.1.2修改主机名1.1.3域名解析1.1.3时间同步1.2mysql安装1.2.1二进制包上传至服务器......
  • MySQL-- NULL值的判断
    前置知识空值即NULL,该值不同于0,也不同于空字符串 字段值是否为空值(NULL)的判断IS[NOT]NULL,其中NOT为可选参数,表示字段值不为空值注意:ISNULL是一个整体,不......
  • CentOS7 安装MySQL教程
    【0】保持网络畅通【1】查看是否已安装MySQLrpm-qa|grepmysql下面是我的操作,可见没有安装MySQL,那么直接进入【2】如果查看出来有东西,可以使用下面命令将其删除(x......
  • 2022-08-18 MySQL常用函数
    MySQL常用函数聚合函数count:计数。count(*)≈count(1)>count(主键)count(*):MySQL对count(*)底层优化,count(0)。count(1)count(主键)count(字段)min:最小值max:最......
  • 2022-08-15 - 初识MySQL
    MySQL数据库数据库数据库,又称为Database,简称DB。数据库就是一个文件集合。顾名思义:是一个存储数据的仓库,实际上就是一堆文件,这些文件中存储了具有特定格式的数据,可以很......
  • Qt6.3.1三种方式远程连接阿里云服务器ECS MySQL数据库(ODBC方式、DSN方式、直连方式)
    一、ODBC方式远程连接MySQL数据库voidcreateMySQLConnByODBC(){qDebug()<<"Qt6支持的数据库驱动有:"<<QSqlDatabase::drivers();QSqlDatabasedb=QSqlDat......
  • 【MySQL】MySQL总结
    目录1.数据库1.1数据库本质1.2数据库分类1.3SQL与NoSQL1.4数据库重要概念1.5数据库存储引擎1.5.1定义1.5.2存储引擎1.5.3不同存储引擎之间底层文件的区别2.针对......