-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec15th.py
34 lines (26 loc) · 994 Bytes
/
dec15th.py
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
import heapq
from typing import List
class Solution:
def incrementalGain(self, passed: int, total: int) -> float:
currentRatio= passed/total
newRatio = (passed + 1)/(total + 1)
return newRatio - currentRatio
def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:
maxHeap = []
for p, t in classes:
gain =self.incrementalGain(p,t)
#largest gain is the smallest negative number
heapq.heappush(maxHeap,(-gain,p,t))
i = 0
while i < extraStudents:
negGain, p, t = heapq.heappop(maxHeap)
p += 1
t += 1
updatedGain = self.incrementalGain(p, t)
heapq.heappush(maxHeap,(-updatedGain, p, t))
i+=1
totalRatioSum = 0.0
i = 0
for i, p, t in maxHeap:
totalRatioSum += p/t
return totalRatioSum/len(classes)