当前位置:网站首页>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 表示日程安排的数量。需要保存所有已经预定的行程。
边栏推荐
- Refinement of the four major collection frameworks: Summary of List core knowledge
- 三子棋讲解(C语言)
- mysql 的简单运用命令
- 剑指offer专项突击版 --- 第 3 天
- 精解四大集合框架:List 核心知识总结
- C语言的文件操作(一)
- 面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
- 剑指offer专项突击版 ---- 第2天
- The process and specific code of sending SMS verification code using flask framework
- <urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
猜你喜欢

C语言文件读、写、定位函数

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

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

MYSQL下载及安装完整教程

Swordsman Offer Special Assault Edition --- Day 3

Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it

C语言实验五 循环结构程序设计(二)
![<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍](/img/a4/8c75fab6a9858c5ddec25f6a8300fb.png)
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍

MySQL-Explain详解

实验8 DNS解析
随机推荐
剑指offer基础版 --- 第24天
uni-app进阶之认证【day12】
tf.keras.utils.pad_sequences()
TOGAF之架构标准规范(一)
[mysql improves query efficiency] Mysql database query is slow to solve the problem
【LeetCode-SQL每日一练】——2. 第二高的薪水
MySQL8.0.26安装配置教程(windows 64位)
Why use Flink and how to get started with Flink?
目标检测学习笔记
第7章 网络层第1次练习题答案(第三版)
闭包(四)----IIFE
C语言实验二 数据类型、运算符和表达式
数据库上机实验1 数据库定义语言
剑指offer专项突击版 --- 第 3 天
数据库上机实验7 数据库设计
剑指offer基础版 ---- 第26天
关于LocalDateTime的全局返回时间带“T“的时间格式处理
Swordsman Offer Special Assault Edition ---- Day 6
详解扫雷游戏(C语言)
C语言教程(二)-printf及c自带的数据类型