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