2
def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:9
# calculate and store impacts for each class in form of tuples -> (-impactValue, passCount, totalCount)11
passCount = classes[i][0]12
totalCount = classes[i][1]14
# calculate the impact for class i15
currentRatio = passCount / totalCount16
expectedRatioAfterUpdate = (passCount + 1) / (totalCount + 1)17
impact = expectedRatioAfterUpdate - currentRatio19
impacts[i] = (-impact, passCount, totalCount) # note the - sign for impact21
heapq.heapify(impacts)23
while extraStudents > 0:24
# pick the next class with greatest impact25
_, passCount, totalCount = heapq.heappop(impacts)27
# assign a student to the class31
# calculate the updated impact for current class32
currentRatio = passCount / totalCount33
expectedRatioAfterUpdate = (passCount + 1) / (totalCount + 1)34
impact = expectedRatioAfterUpdate - currentRatio36
# insert updated impact back into the heap37
heapq.heappush(impacts, (-impact, passCount, totalCount))42
# for all the updated classes calculate the total passRatio43
for _, passCount, totalCount in impacts:44
result += passCount / totalCount46
# return the average pass ratio