Skip to content

Instantly share code, notes, and snippets.

View shrishty's full-sized avatar
🏠
Working from home

Shrishty Chandra shrishty

🏠
Working from home
View GitHub Profile
@shrishty
shrishty / heapq.py
Created October 8, 2021 04:23
Heapq Usage Example
class Solution(object):
def maxAverageRatio(self, classes, extraStudents):
maxHeap = []
def delta(a, b):
return float(a + 1) / (b + 1) — float(a) / b
for a, b in classes:
heapq.heappush(maxHeap, (-delta(a, b), a, b))
for _ in range(extraStudents):
d, a, b = heapq.heappop(maxHeap)
heapq.heappush(maxHeap, (-delta(a+1, b+1), a+1, b+1))