AcWing
  • 首页
  • 题库
  • 题解
  • 分享
  • 问答
  • 活动
  • 应用
  • 吐槽
  • 登录/注册

LeetCode 729. 我的日程安排表1    原题链接    中等

作者: 作者的头像   Stella-W ,  2020-07-18 14:10:42 ,  阅读 232


1


题目描述

题目不难,看到其他的c++题解不够直观

插入每个区间的时候,只需要看两点
(1)start的上一个区间的结尾是否超过了start
(2)start的下一个区间的开头是否被end超过

用map的lower_bound可以求出下一个区间

C++ 代码

class MyCalendar {
public:
    map<int, int> hash;
    MyCalendar() {

    }

    bool book(int start, int end) {
        if (hash.empty())
        {
            hash[start] = end;
            return true;
        } 
        auto up = hash.lower_bound(start);
        if (up != hash.end()) 
            if (up->first < end) return false;
        if (up != hash.begin())
        {
            up--;
            if (up->second > start) return false;
        }
        hash[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);
 */

0 评论

你确定删除吗?

© 2018-2021 AcWing 版权所有  |  京ICP备17053197号-1
联系我们  |  常见问题
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息