Re: [闲聊] 每日LeetCode

楼主: JIWP (JIWP)   2024-02-18 15:45:13
※ 引述《Rushia (みけねこ的鼻屎)》之铭言:
: https://leetcode.com/problems/meeting-rooms-iii/description
: 2402. Meeting Rooms III
: 给你一个数字 n 和一个阵列meetings[],meetings[i] = [starti, endi],n表示你有
: 几个房间,meetings[]表示多个会议的开始时间和结束时间,会议按照以下规则进行安排
: 1.会议会被安排在没人使用且编号最小的房间
: 2.如果没有房间可用,会议会被延迟,消耗的时间和原本的时间相同。
: 3.当延迟的会议有房间可用时,较早开始的会议有较高的优先权。
: 找出哪个房间被用最多次,如果有多个房间使用次数相同返回编号最小的。
思路:
建立一个heap,里面放目前使用的会议室[2]int{结束时间,使用的会议室ID}
rec纪录每个会议室使用次数,chk纪录这个会议室有没有在用
每次都去检查目前在使用的会议室的结束时间是不是比这个会议的开始时间还早
是的话就pop出来
接着检查现在有没有空的会议室
1.没有
从heap pop出一个房间,接着push目前这个会议资讯进到heap
2.有
去找目前没在用的房间ID最小的
并将这个会议资讯push到heap里面
最后去找rec里面值最大的会议室
golang code :
type h [][2]int
func (this h) Len() int { return len(this) }
func (this h) Less(i, j int) bool {
if this[i][0] != this[j][0] {
return this[i][0] < this[j][0]
} else {
return this[i][1] < this[j][1]
}
}
func (this h) Swap(i, j int) { this[i], this[j] = this[j], this[i] }
func (this *h) Pop() interface{} {
n := len(*this)
x := (*this)[n-1]
*this = (*this)[:n-1]
return x
}
func (this *h) Push(x interface{}) {
(*this) = append(*this, x.([2]int))
}
func mostBooked(n int, meetings [][]int) int {
slices.SortFunc[[][]int](meetings, func(a, b []int) int {
return a[0] - b[0]
})
rec := make([]int, n)
h := h{}
idx := 0
chk := make([]bool, n)
for i := 0; i < len(meetings); i++ {
for len(h) > 0 && h[0][0] <= meetings[i][0] {
temp := heap.Pop(&h).([2]int)
chk[temp[1]] = false
}
if len(h) == n { //房间是满的
now := heap.Pop(&h).([2]int)
time := now[0] + meetings[i][1] - meetings[i][0]
rec[now[1]]++
heap.Push(&h, [2]int{time, now[1]})
continue
}
for i := 0; i < n; i++ {
if !chk[i] {
idx = i
break
}
}
rec[idx]++
chk[idx] = true
heap.Push(&h, [2]int{meetings[i][1], idx})
}
ans := -1
max := 0
for i := 0; i < n; i++ {
if rec[i] > max {
max = rec[i]
ans = i
}
}
return ans
}
这个方法满烂的
不过我也懒得想了,我就烂
明天假期结束好忧郁
我没想上班,好想转职
我投的那些职缺能不能给个机会
拜托啦
作者: SecondRun (雨夜琴声)   2024-02-18 15:58:00
球内推
作者: sc95819200 (sc95819200)   2024-02-18 16:01:00
球内推

Links booklink

Contact Us: admin [ a t ] ucptt.com