-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMaximum Average Pass Ratio.js
119 lines (98 loc) · 3.11 KB
/
Maximum Average Pass Ratio.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Runtime: 444 ms (Top 100.0%) | Memory: 91.26 MB (Top 83.3%)
/**
* @param {number[][]} classes
* @param {number} extraStudents
* @return {number}
*/
class MaxHeap {
constructor() {
this.heap = [];
}
push(value) {
this.heap.push(value);
this.heapifyUp(this.heap.length - 1);
}
pop() {
if (this.heap.length === 0) {
return null;
}
if (this.heap.length === 1) {
return this.heap.pop();
}
const top = this.heap[0];
this.heap[0] = this.heap.pop();
this.heapifyDown(0);
return top;
}
heapifyUp(index) {
while (index > 0) {
const parent = Math.floor((index - 1) / 2);
if (this.heap[parent][0] < this.heap[index][0]) {
[this.heap[parent], this.heap[index]] = [this.heap[index], this.heap[parent]];
index = parent;
} else {
break;
}
}
}
heapifyDown(index) {
const n = this.heap.length;
while (true) {
let largest = index;
const left = 2 * index + 1;
const right = 2 * index + 2;
if (left < n && this.heap[left][0] > this.heap[largest][0]) {
largest = left;
}
if (right < n && this.heap[right][0] > this.heap[largest][0]) {
largest = right;
}
if (largest !== index) {
[this.heap[index], this.heap[largest]] = [this.heap[largest], this.heap[index]];
index = largest;
} else {
break;
}
}
}
size() {
return this.heap.length;
}
}
var maxAverageRatio = function(classes, extraStudents) {
const n = classes.length;
// Helper function to calculate pass ratio increase
const passRatioIncrease = (pass, total) => (pass + 1) / (total + 1) - pass / total;
// Initialize max heap to keep track of classes with maximum improvement
const maxHeap = new MaxHeap();
for (let j = 0; j < n; j++) {
const [pass, total] = classes[j];
if (pass !== total) {
const increase = passRatioIncrease(pass, total);
maxHeap.push([increase, j]);
}
}
let totalPassRatio = 0;
for (let j = 0; j < n; j++) {
totalPassRatio += classes[j][0] / classes[j][1];
}
for (let i = 0; i < extraStudents; i++) {
if (maxHeap.size() === 0) {
break; // No more classes to consider
}
const [increase, maxIndex] = maxHeap.pop();
const [pass, total] = classes[maxIndex];
const newPass = pass + 1;
const newTotal = total + 1;
const newIncrease = passRatioIncrease(newPass, newTotal);
const newAvg = (totalPassRatio - (pass / total)) + (newPass / newTotal);
if (newAvg <= totalPassRatio) {
break; // No further improvement possible
}
totalPassRatio = newAvg;
classes[maxIndex][0]++;
classes[maxIndex][1]++;
maxHeap.push([newIncrease, maxIndex]);
}
return totalPassRatio / n;
};