go: add «2594. Minimum Time to Repair Cars»

URL:	https://leetcode.com/problems/minimum-time-to-repair-cars/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-03-16 22:14:00 +01:00
parent f173ae8c3c
commit 356b3e0dde
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,56 @@
package main
import (
"cmp"
pq "github.com/emirpasic/gods/v2/queues/priorityqueue"
)
type Entry struct {
time int64
rank int64
repaired int64
count int64
}
func entryCmp(a, b Entry) int {
return cmp.Compare(a.time, b.time)
}
func repairCars(ranks []int, cars int) int64 {
count := make(map[int64]int64)
for _, rank := range ranks {
rank := int64(rank)
current, ok := count[rank]
if !ok {
current = 0
}
count[rank] = 1 + current
}
q := pq.NewWith(entryCmp)
for rank, m := range count {
q.Enqueue(Entry{
time: rank,
rank: rank,
repaired: 1,
count: m,
})
}
time := int64(0)
for cars > 0 {
next, _ := q.Dequeue()
time = next.time
cars -= int(next.count)
next.repaired++
next.time = next.rank * next.repaired * next.repaired
q.Enqueue(next)
}
return time
}