【华为OD机试真题】95、最少面试官数

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))

}

总结:面试官开始下一场面试时,记得更新该面试官的结束时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值