-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.go
37 lines (34 loc) · 926 Bytes
/
Solution.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
func maxAverageRatio(classes [][]int, extraStudents int) float64 {
pq := hp{}
for _, e := range classes {
a, b := e[0], e[1]
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
heap.Push(&pq, tuple{x, a, b})
}
for i := 0; i < extraStudents; i++ {
e := heap.Pop(&pq).(tuple)
a, b := e.a+1, e.b+1
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
heap.Push(&pq, tuple{x, a, b})
}
var ans float64
for len(pq) > 0 {
e := heap.Pop(&pq).(tuple)
ans += float64(e.a) / float64(e.b)
}
return ans / float64(len(classes))
}
type tuple struct {
x float64
a int
b int
}
type hp []tuple
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool {
a, b := h[i], h[j]
return a.x > b.x
}
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }