当前位置:网站首页>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 表示日程安排的数量。需要保存所有已经预定的行程。
边栏推荐
- 数据库上机实验1 数据库定义语言
- 第7章 网络层第2次练习题答案(第三版)
- C语言实验三 选择结构程序设计
- 剑指offer专项突击版 --- 第 3 天
- 面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
- 实验8 DNS解析
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- C语言教程(一)-准备
- 【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
- a different object with the same identifier value was already associated with the session
猜你喜欢

Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode

Anaconda configure environment directives

MySQL8--Windows下使用压缩包安装的方法

MYSQL一站式学习,看完即学完

MySQL8.0.26安装配置教程(windows 64位)

Quickly master concurrent programming --- the basics
【一起学Rust】Rust学习前准备——注释和格式化输出

剑指offer基础版 ---- 第29天

面试官问我TCP三次握手和四次挥手,我真的是

剑指offer基础版 --- 第24天
随机推荐
闭包(二)
Anaconda configure environment directives
wpf ScrowViewer水平滚动
对list集合进行分页,并将数据显示在页面中
MySQL (updating)
Kubernetes certificate validity period modification
Flask 的初识
精解四大集合框架:List 核心知识总结
uni-app进阶之认证【day12】
Why use Flink and how to get started with Flink?
If the account number or password is entered incorrectly for many times, the account will be banned.
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
C语言实验四 循环结构程序设计(一)
[mysql improves query efficiency] Mysql database query is slow to solve the problem
C语言的文件操作(一)
docker安装postgresSQL和设置自定义数据目录
Flink sink ES 写入 ES(带密码)
目标检测学习笔记
联盟链的真实场景在哪里
一文了解大厂的DDD领域驱动设计