当前位置:网站首页>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 表示日程安排的数量。需要保存所有已经预定的行程。
边栏推荐
猜你喜欢
随机推荐
MySQL8--Windows下使用压缩包安装的方法
uni-app进阶之认证【day12】
Three handshakes and four waves
面试官,不要再问我三次握手和四次挥手
闭包(四)----IIFE
三次握手与四次挥手
C语言教程(一)-准备
Flask 的初识
闭包(二)
初涉C语言
mysql5.7.35安装配置教程【超级详细安装教程】
Kubernetes加入集群的TOKEN值过期
Sword Point Offer Special Assault Edition ---- Day 2
uni-app进阶之内嵌应用【day14】
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
Unity mobile game performance optimization series: performance tuning for the CPU side
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
datagrip带参sql查询
闭包(五)----一个常见的循环
C语言实验一 熟悉C程序的环境







