Implemented hotelbuisness

This commit is contained in:
Egor 2024-06-02 17:19:31 +03:00
parent 750eaed481
commit 7d9db66ec7

View file

@ -2,6 +2,8 @@
package hotelbusiness
import "math"
type Guest struct {
CheckInDate int
CheckOutDate int
@ -13,5 +15,30 @@ type Load struct {
}
func ComputeLoad(guests []Guest) []Load {
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
}