Skip to content

Commit e93c652

Browse files
authored
Update 1792-maximum-average-pass-ratio.js
1 parent fd8f629 commit e93c652

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

1792-maximum-average-pass-ratio.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,111 @@
1+
/**
2+
* @param {number[][]} classes
3+
* @param {number} extraStudents
4+
* @return {number}
5+
*/
6+
const maxAverageRatio = function (classes, extraStudents) {
7+
const pq = new PriorityQueue((a, b) => a.delta > b.delta)
8+
const n = classes.length
9+
for (let e of classes) {
10+
pq.push({
11+
pass: e[0],
12+
total: e[1],
13+
ratio: e[0] / e[1],
14+
delta: (e[0] + 1) / (e[1] + 1) - e[0] / e[1],
15+
})
16+
}
17+
18+
while (extraStudents) {
19+
const tmp = pq.pop()
20+
tmp.pass++
21+
tmp.total++
22+
tmp.ratio = tmp.pass / tmp.total
23+
tmp.delta = (tmp.pass + 1) / (tmp.total + 1) - tmp.ratio
24+
pq.push(tmp)
25+
extraStudents--
26+
}
27+
28+
let res = 0
29+
while (!pq.isEmpty()) {
30+
const tmp = pq.pop()
31+
res += tmp.ratio
32+
}
33+
34+
return res / n
35+
}
36+
37+
class PriorityQueue {
38+
constructor(comparator = (a, b) => a > b) {
39+
this.heap = []
40+
this.top = 0
41+
this.comparator = comparator
42+
}
43+
size() {
44+
return this.heap.length
45+
}
46+
isEmpty() {
47+
return this.size() === 0
48+
}
49+
peek() {
50+
return this.heap[this.top]
51+
}
52+
push(...values) {
53+
values.forEach((value) => {
54+
this.heap.push(value)
55+
this.siftUp()
56+
})
57+
return this.size()
58+
}
59+
pop() {
60+
const poppedValue = this.peek()
61+
const bottom = this.size() - 1
62+
if (bottom > this.top) {
63+
this.swap(this.top, bottom)
64+
}
65+
this.heap.pop()
66+
this.siftDown()
67+
return poppedValue
68+
}
69+
replace(value) {
70+
const replacedValue = this.peek()
71+
this.heap[this.top] = value
72+
this.siftDown()
73+
return replacedValue
74+
}
75+
76+
parent = (i) => ((i + 1) >>> 1) - 1
77+
left = (i) => (i << 1) + 1
78+
right = (i) => (i + 1) << 1
79+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
80+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
81+
siftUp = () => {
82+
let node = this.size() - 1
83+
while (node > this.top && this.greater(node, this.parent(node))) {
84+
this.swap(node, this.parent(node))
85+
node = this.parent(node)
86+
}
87+
}
88+
siftDown = () => {
89+
let node = this.top
90+
while (
91+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
92+
(this.right(node) < this.size() && this.greater(this.right(node), node))
93+
) {
94+
let maxChild =
95+
this.right(node) < this.size() &&
96+
this.greater(this.right(node), this.left(node))
97+
? this.right(node)
98+
: this.left(node)
99+
this.swap(node, maxChild)
100+
node = maxChild
101+
}
102+
}
103+
}
104+
105+
106+
107+
// another
108+
1109
/**
2110
* @param {number[][]} classes
3111
* @param {number} extraStudents

0 commit comments

Comments
 (0)