首页 > 其他分享 >29. My Calendar I

29. My Calendar I

时间:2023-03-07 13:04:05浏览次数:32  
标签:end 29 start book booking Calendar My event MyCalendar

29. My Calendar I

标签(空格分隔): leetcode array medium


题目

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)

Example 1:

MyCalendar(); MyCalendar.book(10, 20); // returns true MyCalendar.book(15, 25); // returns false MyCalendar.book(20, 30); // returns true Explanation: The first event can be booked. The second can't because time 15 is already booked by another event. The third event can be booked, as the first event takes every time less than 20, but not including 20.

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].

  • 思路

    本题定义一个数值对数组A存储{start,end},要想发生double booking,必须满足当前的
    max(start,A.start)<min(end,A.end)

    代码
    class MyCalendar {
    public:
        vector<pair<int,int>> books;
        MyCalendar() {
            
        }
        
        bool book(int start, int end) {
            for(auto item:books)
            {
                if(max(item.first,start)<min(item.second,end))
                    return false;
            }
            books.push_back({start,end});   
            return true;
        }
    };
    
    /**
     * Your MyCalendar object will be instantiated and called as such:
     * MyCalendar obj = new MyCalendar();
     * bool param_1 = obj.book(start,end);
     */
    

标签:end,29,start,book,booking,Calendar,My,event,MyCalendar
From: https://blog.51cto.com/u_15996214/6105921

相关文章

  • 731. My Calendar II
    题目ImplementaMyCalendarTwoclasstostoreyourevents.Aneweventcanbeaddedifaddingtheeventwillnotcauseatriplebooking.Yourclasswil......
  • 229. Majority Element II
    #题目Givenanintegerarrayofsizen,findallelementsthatappearmorethan⌊n/3⌋times.ThealgorithmshouldruninlineartimeandinO(1)space.#......
  • MyBatis 动态SQL标签汇总
    if标签<selectid="getEmpByCondition"resultType="com.xy.mybatis.dynamic.pojo.Emp">select*fromempwhere1=1<iftest="name!=''andname!=......
  • mybatis-plus(mp)常用概念以及demo实操
    概念:作为国内流行的持久层框架,mp是mybatis的拓展,并不改变mybaits的底层,因此使用mybatis的项目可以无缝使用mp进行迭代本博客旨在温习mp常用的使用场景、使用方式(一)demo框......
  • 老杜带你从零入门MyBatis,学MyBatis看这篇就够了!
    MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundation迁移到了googlecode,并且改名为MyBatis。2013年11月迁移到Github。iBATIS一词来源于......
  • Mybatis框架的mapper接口中的方法名可以重载吗
    关于mybatis框架的mapper接口中的方法名是否可以重载答案是不可以重载为什么是不可以重载?这个就要从Mybatis框架中mapper接口的工作原理说起Mybatis中mapper接口的工作......
  • MybatisPlus多表连接查询
    一、(一)背景内容软件应用技术架构中DAO层最常见的选型组件为MyBatis,熟悉MyBatis的朋友都清楚,曾几何时MyBatis是多么的风光,使用XML文件解决了复杂的数据库访问的难题。时至......
  • Mysql 主从复制和 GTID 复制
    1、安装主Mysql优化命令创建数据和日志存储目录1)安装Mysql​root@centos05~]#tarzxfmysql-8.0.32-el7-x86_64.tar.gz-C/usr/src/[root@centos05~]#mv/usr/src/m......
  • MySQL 安装过程中踩过的坑
    1、用 grep'temporarypassword'/var/log/mysqld.log生成的初始密码老提示密码错误,只能直接发大招:       A、vi/etc/my.cnf在文件的[mysqld]内增加一行 ......
  • mysql 0点 弹窗 取消
         ......