【LeetCode】732. My Calendar III解题报告
【LeetCode】732. My Calendar III解题报告
标签(空格分隔): LeetCode
题目地址:https://leetcode.com/problems/my-calendar-iii/description/
题目描述:
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].
解题方法
看这个:https://www.cnblogs.com/FannyChung/p/7896415.html
代码:
class Node(object):
def __init__(self, start, end, c):
self.start = start
self.end = end
self.count = c
self.left = None
self.right = None
class MyCalendarThree(object):
def __init__(self):
self.root = None
self.maxK = 1
def book_helper(self, root, start, end, c):
if root == None:
return Node(start, end, c)
if start >= root.end:
#不能写成return self.boook_helper(),因为要进行树的构建和修改,一定要赋值给root.right
root.right = self.book_helper(root.right, start, end, c)
elif end <= root.start:
root.left = self.book_helper(root.left, start, end, c)
else:
intervals = sorted([start, end, root.start, root.end])
root_l, root_r = root.start, root.end
root.start, root.end = intervals[1], intervals[2]
root.left = self.book_helper(root.left, intervals[0], intervals[1], c if start <= root_l else root.count)
root.right = self.book_helper(root.right, intervals[2], intervals[3], c if end >= root_r else root.count)
root.count += c
self.maxK = max(root.count, self.maxK)
return root
def book(self, start, end):
"""
:type start: int
:type end: int
:rtype: int
"""
self.root = self.book_helper(self.root, start, end, 1)
return self.maxK
# Your MyCalendarThree object will be instantiated and called as such:
# obj = MyCalendarThree()
# param_1 = obj.book(start,end)
日期
2018 年 2 月 25 日
【LeetCode】732. My Calendar III解题报告的更多相关文章
- LeetCode 732. My Calendar III
原题链接在这里:https://leetcode.com/problems/my-calendar-iii/ 题目: Implement a MyCalendarThree class to stor ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】729. My Calendar I 解题报告
[LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
随机推荐
- LVS-三种模式的配置详情
NAT模式 实验环境 LVS1 VIP 192.168.31.66 DIP 192.168.121.128 WEB1 192.168.121.129 WEB2 192.168.121.130 安装与配 ...
- python基础实战
字符串的互相转换 字典的排序 字典的排序可以直接把,key值或者,values拿出来排序 也可以用dict.items拿出所有的key,value的值再加key=lambda x:x[1] 来排序. ...
- 学习java的第十二天
一.今日收获(前两天家里有事,博客都忘了发了,唉) 1.通过看哔哩哔哩看黑马程序员的教学视频,学习了java中的数据类型自动转换.强制转换及注意事项三节 2.简单看了看完全学习手册 二.今日问题 1. ...
- 日常Java 2021/11/16
获得applet参数 下面的例子演示了如何使用一个Applet响应来设置文件中指定的参数.该Applet显示了-个黑色棋盘图案和第二种颜色.第二种颜色和每一列的大小通过文档中的Applet的参数指定. ...
- 学习java 7.21
学习内容: 模块使用 AWT是窗口框架 它从不同平台的窗口系统中抽取出共同组件,当程序运行时,将这些组件的创建和动作委托给程序所在的运行平台.简而言之,当使用AWT编写图形界面应用时,程序仅指定了界面 ...
- 零基础学习java------day18------properties集合,多线程(线程和进程,多线程的实现,线程中的方法,线程的声明周期,线程安全问题,wait/notify.notifyAll,死锁,线程池),
1.Properties集合 1.1 概述: Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串 一个属性列表可包含另 ...
- 【Reverse】初遇花指令
解密花指令 全文参考了一个大师傅的blog:https://blog.csdn.net/zhangmiaoping23/article/details/38400393 介绍 花指令是对抗反汇编的有效 ...
- 2.8 rust 枚举与模式匹配
Enums and Pattern Matching 摘要 枚举定义 enum IpAddrKind { V4, V6, } 枚举方法 fn main() { enum Message { Quit, ...
- Git命令行演练-团队开发
** 团队开发必须有一个共享库,这样成员之间才可以进行协作开发** ### 0. 共享库分类 > 本地共享库(只能在本地面对面操作) - 电脑文件夹/U盘/移动硬盘 & ...
- 【C/C++】指针,传参,引用的一些个人理解。
(以下均为个人理解) 函数访问的传参两种方式大致为: 值传递: 地址传递. 但是实际上可以都理解为,传进来的[形参]是主函数里的实参值的[一种复制]. 举个例子,哪怕我们将地址作为子函数的输入变量,形 ...