//原题链接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. Flink - [01] 概述

    官网:https://flink.apache.org/ 一.Flink 是什么 是一个流式的数据流执行引擎,其针对数据流的分布式计算提供了数据分布,数据通信以及容错机制等功能. 是一个框架和分布式处 ...

  2. 赶快检查,木马可能已经植入服务器,Redis未授权访问漏洞记录,redis的key值出现backup要谨慎

    问题描述:为图省事,很多时候我们在使用redis的时候会使用默认空密码,这就增加了安全隐患,如果有下属情况,那赶快去检查下redis,木马或许已经植入服务器,应尽快处理: 1.redis绑定在 0.0 ...

  3. 不止排名,Google SEO 10 大核心心得分享

    原博客:https://bysocket.com/seo-tips-2025/ 在过去的一年中,我深入实践了 Google SEO,积累了自己一些经验和看法.以下是我的实操心得,希望对大家有所帮助. ...

  4. Windows编程----进程:命令行参数

    什么是进程的命令行参数 每个进程在启动(双击exe启动.cmd命令行启动或者由其他程序通过CreateProcess启动)的时候,都会有一个命令行参数给它.命令行的参数以空格区分.这个命令行总是不为空 ...

  5. python excel 数据整理:如何删除重复的记录

    data = frame.drop_duplicates(subset='', keep='first', inplace='') drop_duplicates用法:subset='需要去重复的列名 ...

  6. containerd 配置使用私有镜像仓库 harbor

    前言 ​当要从非安全的镜像仓库中进行 Pull.Push 时,会遇到 x509: certificate signed by unknown authority 错误提示: 这是由于镜像仓库是可能是 ...

  7. Java使用多线程处理未知任务数方案

    知道任务个数,你可以定义好线程数规则,生成线程数去跑 代码说明: 虚拟线程池: 使用 Executors.newVirtualThreadPerTaskExecutor() 创建虚拟线程池,每个任务将 ...

  8. Delphi WebBrowser内核版本修改D7

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

  9. Linux+Typora+Picgo图床配置

    Linux+Typora+Picgo图床配置 首先不建议安装在UbuntuStore里的版本,会有一些限制. 首先安装node.js 去官网下载编译好的源码,配置软连接,使全局都可以使用node命令. ...

  10. study PostgreSQL【1-PostgreSQL对象】

    1.服务 PostgreSQL是作为一种服务安装在操作系统下.多个PostgreSQL服务可以运行于同一台问你服务器上,但是他们侦听端口不能重复,也不能共享同一个数据存储目录. 2.Database ...