forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0857.go
49 lines (41 loc) · 1.08 KB
/
0857.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
type FloatHeap []float64
func (h FloatHeap) Len() int {
return len(h)
}
func (h FloatHeap) Less(i, j int) bool {
return h[i] < h[j]
}
func (h FloatHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *FloatHeap) Pop() interface{} {
x := (*h)[(*h).Len()-1]
*h = (*h)[:(*h).Len()-1]
return x
}
func (h *FloatHeap) Push(x interface{}) {
*h = append(*h, x.(float64))
}
func mincostToHireWorkers(q []int, w []int, K int) float64 {
workers := make([][]float64, len(q))
for i := 0; i < len(q); i++ {
workers[i] = []float64{float64(w[i]) / float64(q[i]), float64(q[i])}
}
sort.Slice(workers, func(i, j int) bool {
return workers[i][0] < workers[j][0]
})
var res, qsum float64
res, qsum = 1000000000, 0
pq := new(FloatHeap)
for _, worker := range workers {
qsum += worker[1]
if len(*pq) == K - 1 {
res = math.Min(res, qsum * worker[0])
}
heap.Push(pq, -worker[1])
if len(*pq) >= K {
qsum += heap.Pop(pq).(float64)
}
}
return res
}