-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1792-maximum-average-ratio.dart
128 lines (115 loc) · 2.68 KB
/
1792-maximum-average-ratio.dart
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
120
121
122
123
124
125
126
127
128
class Solution {
double maxAverageRatio(List<List<int>> classes, int extraStudents) {
double improvement(int pass, int total) {
return (pass + 1) / (total + 1) - pass / total;
}
List<List<dynamic>> maxHeap = [];
void heapifyUp(int index) {
while (index > 0) {
int parent = (index - 1) ~/ 2;
if (maxHeap[index][2] > maxHeap[parent][2]) {
var temp = maxHeap[index];
maxHeap[index] = maxHeap[parent];
maxHeap[parent] = temp;
index = parent;
} else {
break;
}
}
}
void heapifyDown(int index) {
int size = maxHeap.length;
while (true) {
int left = 2 * index + 1;
int right = 2 * index + 2;
int largest = index;
if (left < size && maxHeap[left][2] > maxHeap[largest][2]) {
largest = left;
}
if (right < size && maxHeap[right][2] > maxHeap[largest][2]) {
largest = right;
}
if (largest != index) {
var temp = maxHeap[index];
maxHeap[index] = maxHeap[largest];
maxHeap[largest] = temp;
index = largest;
} else {
break;
}
}
}
for (var cls in classes) {
int pass = cls[0], total = cls[1];
maxHeap.add([pass, total, improvement(pass, total)]);
heapifyUp(maxHeap.length - 1);
}
for (int i = 0; i < extraStudents; i++) {
var topClass = maxHeap[0];
int pass = topClass[0], total = topClass[1];
pass += 1;
total += 1;
maxHeap[0] = [pass, total, improvement(pass, total)];
heapifyDown(0);
}
double totalRatio = 0.0;
for (var cls in maxHeap) {
totalRatio += cls[0] / cls[1];
}
return totalRatio / classes.length;
}
}
void main() {
var solution = Solution();
print(solution.maxAverageRatio([
[1, 2],
[3, 5],
[2, 2]
], 2)); // Output: 0.78333
print(solution.maxAverageRatio([
[2, 4],
[3, 9],
[4, 5],
[2, 10]
], 4)); // Output: 0.53485
var classes = [
[129, 381],
[729, 1000],
[15, 551],
[207, 789],
[276, 638],
[67, 830],
[474, 804],
[13, 291],
[273, 663],
[743, 985],
[514, 709],
[23, 580],
[115, 412],
[101, 702],
[330, 620],
[635, 653],
[705, 713],
[587, 951],
[459, 909],
[94, 626],
[511, 543],
[352, 604],
[696, 895],
[45, 193],
[57, 174],
[120, 632],
[44, 826],
[121, 569],
[313, 543],
[772, 804],
[166, 206],
[154, 547],
[234, 887],
[92, 223],
[362, 516],
[707, 920],
[577, 861],
];
print(solution.maxAverageRatio(classes, 100)); // Optimized for large inputs.
}