当前位置:网站首页>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 表示日程安排的数量。需要保存所有已经预定的行程。
边栏推荐
- 闭包(三)----执行环境
- Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
- Kubernetes certificate validity period modification
- 三次握手与四次挥手
- Input length must be multiple of 8 when decrypting with padded cipher
- C语言实验一 熟悉C程序的环境
- uni-app进阶之自定义【day13】
- 【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
- Linux系统安装mysql(rpm方式安装)
- C语言如何分辨大小端
猜你喜欢

MySQL_关于JSON数据的查询

剑指offer专项突击版 ---- 第2天

第7章 网络层第3次练习题答案(第三版)

基于flask的三方登陆的流程

一文了解大厂的DDD领域驱动设计

详解扫雷游戏(C语言)

About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

剑指offer专项突击版 ---第 5 天

Proteus 8 Professional安装教程

Kubernetes certificate validity period modification
随机推荐
【C语言3个基本结构详解——顺序、选择、循环】
Qt Creator + CMake 运行调试总会自动 build 所有目标
find、filter、map的区别
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
Refinement of the four major collection frameworks: Summary of List core knowledge
【MQ我可以讲一个小时】
C语言实验二 数据类型、运算符和表达式
Linux系统安装mysql(rpm方式安装)
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
wpf wrapPanel居中并从左到右排列
torch.normal function usage
闭包(四)----IIFE
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
torch.normal函数用法
tf.keras.utils.get_file()
MySQL (updating)
Sword Point Offer Special Assault Edition ---- Day 2
uni-app进阶之模版语法与数据绑定【day7】
C语言实验一 熟悉C程序的环境