//原题链接https://leetcode.com/problems/my-calendar-iii/submissions/

  • 题目描述

    Implement a MyCalendarThree class to store your events. A new event can always be added.

    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.

    A K-booking happens when K events have some non-empty intersection (ie., there is some time that is common to all K events.)

    For each call to the method MyCalendar.book, return an integer K representing the largest integer such that there exists a K-booking in the calendar.

    Your class will be called like this: MyCalendarThree cal = new MyCalendarThree(); MyCalendarThree.book(start, end)

    Example 1:

    MyCalendarThree();
    MyCalendarThree.book(10, 20); // returns 1
    MyCalendarThree.book(50, 60); // returns 1
    MyCalendarThree.book(10, 40); // returns 2
    MyCalendarThree.book(5, 15); // returns 3
    MyCalendarThree.book(5, 10); // returns 3
    MyCalendarThree.book(25, 55); // returns 3
    Explanation:
    The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking.
    The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.
    The remaining events cause the maximum K-booking to be only a 3-booking.
    Note that the last event locally causes a 2-booking, but the answer is still 3 because
    eg. [10, 20), [10, 40), and [5, 15) are still triple booked.

    Note:
    The number of calls to MyCalendarThree.book per test case will be at most 400.
    In calls to MyCalendarThree.book(start, end), start and end are integers in the range [0, 10^9].

  • 思路分析
    每次向时间轴里插入一个有向线段,返回此时最多的重合次数
    class MyCalendarThree {
    TreeMap<Integer,Integer> timeline = new TreeMap<Integer,Integer>(); public MyCalendarThree() { } public int book(int start, int end) {
    timeline.put(start,timeline.containsKey(start) ? timeline.get(start)+1 : 1);
    timeline.put(end,timeline.containsKey(end) ? timeline.get(end)-1 :-1); int result = 0,cur = 0;
    for(int i:timeline.values()){
    result = Math.max(result,cur=cur+i);
    } return result;
    }
    } /**
    * Your MyCalendarThree object will be instantiated and called as such:
    * MyCalendarThree obj = new MyCalendarThree();
    * int param_1 = obj.book(start,end);
    */

My Calendar III——LeetCode⑪的更多相关文章

  1. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  2. [LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 题解

    题目描述 MyCalendar主要实现一个功能就是插入指定起始结束时间的事件,对于重合的次数有要求. MyCalendar I要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...

  3. [LeetCode] My Calendar III 我的日历之三

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  4. LeetCode 732. My Calendar III

    原题链接在这里:https://leetcode.com/problems/my-calendar-iii/ 题目: Implement a MyCalendarThree class to stor ...

  5. [Swift]LeetCode732. 我的日程安排表 III | My Calendar III

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  6. House Robber III leetcode 动态规划

    https://leetcode.com/submissions/detail/56095603/ 这是一道不错的DP题!自己想了好久没有清晰的思路,参看大神博客!http://siukwan.sin ...

  7. Best Time to Buy and Sell Stock I,II,III [leetcode]

    Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...

  8. My Calendar III

    class MyCalendarThree(object): """ Implement a MyCalendarThree class to store your ev ...

  9. Segment Tree-732. My Calendar III

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  10. 732. My Calendar III (prev)

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

随机推荐

  1. 傻妞教程——对接QQ机器人go-cqhttp

    原本我懒,用的傻妞QQbot一键安装版,docker的,最近有点问题,索性换了go-cqhttp 安装go-cqhttp: go-cqhttp项目地址:https://github.com/Mrs4s ...

  2. FolderMove:盘符文件/软件迁移工具,快速给C盘瘦身

    前言 很多朋友安装软件的时候总会直接点击下一步,每次都把软件安装到了C盘.时间长了以后系统C盘就会爆满,只能重做系统处理,有了这个软件就可以随时把C盘文件转移到其他分区 介绍 这款是国外软件,界面介绍 ...

  3. Ansible - [04] 关于sudo的一些配置

    sudo sudo,以超级管理员或其他人的身份执行命令 基本流程 管理员需要先授权(修改/etc/sudoers文件) 普通用户以sudo的形式执行命令 可以通过sudo -l查看授权情况 配置sud ...

  4. Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露

    一:背景 1. 讲故事 前面跟大家分享过一篇 C# 调用 C代码引发非托管内存泄露 的文章,这是一个故意引发的正向泄露,这一篇我们从逆向的角度去洞察引发泄露的祸根代码,这东西如果在 windows 上 ...

  5. CTF-CRYPTO-ECC(1)

    CTF-CRYPTO-ECC(1) 椭圆加密 1.简介 椭圆曲线密码学(Elliptic curve cryptography),简称 ECC,和RSA.ElGamel 算法等类似,是一种公开秘钥加密 ...

  6. ant-design-pro 自定义表单 rules规则

    表单输入 <ProFormText name="id" label={intl.formatMessage({ id: 'pages.secret.form.id' })} ...

  7. Delphi WebBrowser内核版本修改D7

    private { Private declarations } public { Public declarations } function WriteAppNameToReg:Boolean; ...

  8. 【Java】异常处理

    异常的定义 异常:在Java语言中,将程序执行中发生的不正常情况称为"异常". (开发过程中的语法错误和逻辑错误不是异常) 1. 异常的体系结构 Java程序在执行过程中所发生的异 ...

  9. 如何在Ubuntu系统中重置root密码

    很多人有个问题,就是喜欢把密码设置得很长很复杂,结果谁也没防住,却成功防住了自己 ヽ(.◕ฺˇд ˇ◕ฺ;)ノ 对于现代人,特别是年轻人,都有过忘记密码的经历吧.在这篇文章中,我们来了解如何在 Ubu ...

  10. 查缺补漏——01-BFS

    01bfs 解决的是一类特殊的最段路问题. 在学习它的过程中,我更加深刻地学习到了泛化路径和 bfs. 01-BFS 是什么 首先明确,01-BFS 是一种图论算法.它解决的事最短路径问题.最短路径算 ...