[LeetCode] My Calendar III 我的日历之三
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.bookper test case will be at most400. - In calls to
MyCalendarThree.book(start, end),startandendare integers in the range[0, 10^9].
这道题是之前那两道题My Calendar II,My Calendar I的拓展,论坛上有人说这题不应该算是Hard类的,但实际上如果没有之前那两道题做铺垫,直接上这道其实还是还蛮有难度的。这道题博主在做完之前那道,再做这道一下子就做出来了,因为用的就是之前那道My Calendar II的解法二,具体的讲解可以参见那道题,反正博主写完那道题再来做这道题就是秒解啊,参见代码如下:
class MyCalendarThree {
public:
MyCalendarThree() {}
int book(int start, int end) {
++freq[start];
--freq[end];
int cnt = , mx = ;
for (auto f : freq) {
cnt += f.second;
mx = max(mx, cnt);
}
return mx;
}
private:
map<int, int> freq;
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/111978/java-c-clean-code
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] My Calendar III 我的日历之三的更多相关文章
- [LeetCode] My Calendar I 我的日历之一
Implement a MyCalendar class to store your events. A new event can be added if adding the event will ...
- [LeetCode] My Calendar II 我的日历之二
Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- [LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 题解
题目描述 MyCalendar主要实现一个功能就是插入指定起始结束时间的事件,对于重合的次数有要求. MyCalendar I要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...
- LeetCode My Calendar I
原题链接在这里:https://leetcode.com/problems/my-calendar-i/description/ 题目: Implement a MyCalendar class to ...
- 与众不同 windows phone (26) - Contacts and Calendar(联系人和日历)
原文:与众不同 windows phone (26) - Contacts and Calendar(联系人和日历) [索引页][源码下载] 与众不同 windows phone (26) - Con ...
- [LeetCode] 731. My Calendar II 我的日历之二
Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...
随机推荐
- [poj2923]Relocation_状压dp_01背包
Relocation poj-2923 题目大意:给出n个物品,有两辆车,两辆车必须一起出动并且每辆车有单独的容量.问最少需要运输多少次才能运走所有货物. 注释:n<=10,容量,物品代价< ...
- 配置COCO API(安装COCO)
仍旧是win10,Python3.5 从GitHub下载coco源码,解压到任意文件夹.(或者创建一个工程)coco源码链接 https://github.com/cocodataset/cocoap ...
- JAVA多线程实现和应用总结
1.JAVA多线程实现方式JAVA多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中 ...
- mybatis整合oracle 实现一对多查询 备注?
<resultMap type="com.asiainfo.channel.model.weeklyNewspaper.WorkReportInfo" id="Wo ...
- 高校学生征信系统Postmortem结果
Postmortem结果 设想和目标 1 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件需要解决的问题是当前高校学生征信系统建设薄弱的问题,我们试图建立 ...
- 20145237 《Java程序设计》第4周学习总结
20145237 <Java程序设计>第4周学习总结 教材学习内容总结 一. 继承 . 定义:面向对象中,涉及对象过多时容易出现重复行为,为了避免这种情况,就可把重复的部分写成父类,由子 ...
- 关于使用栈将一般运算式翻译为后缀表达式并实现三级运算的方法及实例(cpp版)
#include <iostream> #include <stack> #include <vector> #include <string> #de ...
- socket_sever实现多客户端并发
#!/usr/bin/env python # -*- coding:utf-8 -*- import socketserver class mysever(socketserver.BaseRequ ...
- 本地通知UILocalNotification
1.增加一个本地推送 //设置20秒之后 ]; //chuagjian一个本地推送 UILocalNotification *noti = [[[UILocalNotification alloc] ...
- RxSwift(一)
文/iOS_Deve(简书作者) 原文链接:http://www.jianshu.com/p/429b5160611f 著作权归作者所有,转载请联系作者获得授权,并标注"简书作者" ...