2
* @param {number[][]} classes3
* @param {number} extraStudents12
this.heap.push(value);13
this.heapifyUp(this.heap.length - 1);17
if (this.heap.length === 0) {20
if (this.heap.length === 1) {21
return this.heap.pop();24
const top = this.heap[0];25
this.heap[0] = this.heap.pop();33
const parent = Math.floor((index - 1) / 2);34
if (this.heap[parent][0] < this.heap[index][0]) {35
[this.heap[parent], this.heap[index]] = [47
const n = this.heap.length;50
const left = 2 * index + 1;51
const right = 2 * index + 2;53
if (left < n && this.heap[left][0] > this.heap[largest][0]) {56
if (right < n && this.heap[right][0] > this.heap[largest][0]) {60
if (largest !== index) {61
[this.heap[index], this.heap[largest]] = [73
return this.heap.length;77
var maxAverageRatio = function (classes, extraStudents) {78
const n = classes.length;80
// Helper function to calculate pass ratio increase81
const passRatioIncrease = (pass, total) =>82
(pass + 1) / (total + 1) - pass / total;84
// Initialize max heap to keep track of classes with maximum improvement85
const maxHeap = new MaxHeap();87
for (let j = 0; j < n; j++) {88
const [pass, total] = classes[j];90
const increase = passRatioIncrease(pass, total);91
maxHeap.push([increase, j]);95
let totalPassRatio = 0;97
for (let j = 0; j < n; j++) {98
totalPassRatio += classes[j][0] / classes[j][1];101
for (let i = 0; i < extraStudents; i++) {102
if (maxHeap.size() === 0) {103
break; // No more classes to consider106
const [increase, maxIndex] = maxHeap.pop();107
const [pass, total] = classes[maxIndex];108
const newPass = pass + 1;109
const newTotal = total + 1;110
const newIncrease = passRatioIncrease(newPass, newTotal);111
const newAvg = totalPassRatio - pass / total + newPass / newTotal;113
if (newAvg <= totalPassRatio) {114
break; // No further improvement possible117
totalPassRatio = newAvg;118
classes[maxIndex][0]++;119
classes[maxIndex][1]++;120
maxHeap.push([newIncrease, maxIndex]);123
return totalPassRatio / n;