当前位置:网站首页>leetcode-每日一题731. 我的日程安排表 II
leetcode-每日一题731. 我的日程安排表 II
2022-07-31 05:10:00 【lin钟一】

题目链接:https://leetcode.cn/problems/my-calendar-ii/
孪生弟弟题 729. 我的日程安排表 I:https://leetcode.cn/problems/my-calendar-i/
思路
方法一、直接遍历
直接想法
题目需要我们判断在重复的预定时间里,有三重预定的返回false,那我们可以定义一个pair结构体用来表示时间段,MyCalendarTwo结构体用来存成功预定的时间段booked切片,和有重复预定的时间段overlaps切片,overlaps切片用来判断新进的时间段是否跟overlaps有重合预定的情况,若有则返回false,没有则true
代码示例
type pair struct{
start, end int }
type MyCalendarTwo struct{
booked, overlaps []pair }
func Constructor() MyCalendarTwo {
return MyCalendarTwo{
}
}
func (c *MyCalendarTwo) Book(start, end int) bool {
for _, p := range c.overlaps {
if p.start < end && start < p.end {
return false
}
}
for _, p := range c.booked {
if p.start < end && start < p.end {
c.overlaps = append(c.overlaps, pair{
max(p.start, start), min(p.end, end)})
}
}
c.booked = append(c.booked, pair{
start, end})
return true
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
func max(a, b int) int {
if b > a {
return b
}
return a
}

复杂度分析
时间复杂度:O(n2)其中 n 表示日程安排的数量。由于每次在进行预定时,都需要遍历所有已经预定的行程安排。
空间复杂度:O(n),其中 n 表示日程安排的数量。需要保存所有已经预定的行程。
边栏推荐
- 太厉害了,终于有人能把文件上传漏洞讲的明明白白了
- MYSQL下载及安装完整教程
- 剑指offer基础版 ----第31天
- The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
- Three handshakes and four waves
- 变量的解构赋值
- The interviewer asked me TCP three handshake and four wave, I really
- 踏上编程之路,你必须要干的几件事
- MySQL8--Windows下使用压缩包安装的方法
- 闭包(二)
猜你喜欢

关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

【C语言3个基本结构详解——顺序、选择、循环】

剑指offer基础版--- 第23天
uni-app进阶之模版语法与数据绑定【day7】

Anaconda配置环境指令
【一起学Rust】Rust的Hello Rust详细解析
![<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍](/img/a4/8c75fab6a9858c5ddec25f6a8300fb.png)
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍

MySQL-如何分库分表?一看就懂

基于flask的三方登陆的流程

mysql5.7.35安装配置教程【超级详细安装教程】
随机推荐
C语言教程(二)-printf及c自带的数据类型
Flink sink redis writes to Redis
如何将项目部署到服务器上(全套教程)
【C语言趣味小游戏——猜数字】
Unity mobile game performance optimization series: performance tuning for the CPU side
The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
账号或密码多次输入错误,进行账号封禁
Mysql——字符串函数
实验8 DNS解析
tf.keras.utils.pad_sequences()
Three handshakes and four waves
字符串的扩展
The interviewer asked me TCP three handshake and four wave, I really
Kubernetes 证书可用年限修改
联盟链的真实场景在哪里
剑指offer基础版 --- 第21天
Object Detection Study Notes
Redis的初识
【LeetCode-SQL每日一练】——2. 第二高的薪水
C语言教程(三)-if和循环