package main
import (
"fmt"
"sort"
)
type s struct {
start int
end int
workCount int
}
type duration struct {
start int
end int
}
// 查询时间段内是否有可用的面试官
func getFreeS(sList []*s, d *duration, workCountLimit int) (sIndex int) {
sIndex = -1
if len(sList) == 0 {
return sIndex
}
for i, sItem := range sList {
if sItem.end <= d.start {
if sItem.workCount < workCountLimit {
sIndex = i
break
}
}
}
return
}
func main() {
var workCountLimit int
fmt.Scan(&workCountLimit)
var m int
fmt.Scan(&m)
durationList := make([]*duration, m)
for i := 0; i < m; i++ {
var star, end int
fmt.Scan(&star, &end)
durationList[i] = &duration{
start: star,
end: end,
}
}
sort.Slice(durationList, func(i, j int) bool {
if durationList[i].start != durationList[j].start {
return durationList[i].start < durationList[j].start
} else {
return durationList[i].end < durationList[j].end
}
})
sList := make([]*s, 0)
for i, d := range durationList {
if i == 0 {
sList = append(sList, &s{
start: d.start,
end: d.end,
workCount: 1,
})
} else {
if sIndex := getFreeS(sList, d, workCountLimit); sIndex > -1 {
//目前用空闲的面试官
sList[sIndex].workCount++
sList[sIndex].start = d.start
sList[sIndex].end = d.end
} else {
//需要增加一个面试官
sList = append(sList, &s{
start: d.start,
end: d.end,
workCount: 1,
})
}
}
}
fmt.Println(len(sList))
}
总结:面试官开始下一场面试时,记得更新该面试官的结束时间