shad-go/hotelbusiness/hotels.go

45 lines
876 B
Go
Raw Normal View History

2022-02-10 22:06:57 +00:00
//go:build !solution
2020-02-24 19:54:48 +00:00
package hotelbusiness
2024-06-02 14:19:31 +00:00
import "math"
2020-02-24 19:54:48 +00:00
type Guest struct {
CheckInDate int
CheckOutDate int
}
type Load struct {
StartDate int
GuestCount int
}
func ComputeLoad(guests []Guest) []Load {
2024-06-02 14:19:31 +00:00
if len(guests) == 0 {
return []Load{}
}
minDate, maxDate := math.MaxInt, math.MinInt
loadsNoEmptyDays := make(map[int]int)
for _, guest := range guests {
if guest.CheckInDate < minDate {
minDate = guest.CheckInDate
}
if guest.CheckOutDate > maxDate {
maxDate = guest.CheckOutDate
}
for i := guest.CheckInDate; i < guest.CheckOutDate; i++ {
loadsNoEmptyDays[i]++
}
}
loads, prevGuestCount := make([]Load, 0), 0
for i := minDate; i <= maxDate; i++ {
guestCount := loadsNoEmptyDays[i]
if prevGuestCount == guestCount {
continue
}
loads = append(loads, Load{i, guestCount})
prevGuestCount = guestCount
}
return loads
2020-02-24 19:54:48 +00:00
}