Skip to content

Commit

Permalink
Dec 15
Browse files Browse the repository at this point in the history
  • Loading branch information
siddydutta committed Dec 15, 2024
1 parent 94273d0 commit e9feff5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from heapq import heapify, heappop, heappush
from typing import List


class Solution:
def maxAverageRatio(
self, classes: List[List[int]], extraStudents: int
) -> float:
def profit(a, b):
return (a + 1) / (b + 1) - a / b

max_heap = []
for a, b in classes:
max_heap.append((-profit(a, b), a, b))
heapify(max_heap)

for _ in range(extraStudents):
_, a, b = heappop(max_heap)
heappush(max_heap, (-profit(a + 1, b + 1), a + 1, b + 1))

return sum(a / b for _, a, b in max_heap) / len(classes)


def main():
classes = [[1, 2], [3, 5], [2, 2]]
extraStudents = 2
result = Solution().maxAverageRatio(classes, extraStudents)
assert abs(result) < 1e-5

classes = [[2, 4], [3, 9], [4, 5], [2, 10]]
extraStudents = 4
result = Solution().maxAverageRatio(classes, extraStudents)
assert abs(result) < 1e-5


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions 2024-12-December-LeetCoding-Challenge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
| December 12 | [2558. Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/) | Easy | Solved |
| December 13 | [2593. Find Score of an Array After Marking All Elements](https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/) | Medium | Solved |
| December 14 | [2762. Continuous Subarrays](https://leetcode.com/problems/continuous-subarrays/) | Medium | Unsolved |
| December 15 | []() | | |
| December 15 | [1792. Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/) | Medium | Unsolved |
| December 16 | []() | | |
| December 17 | []() | | |
| December 18 | []() | | |
Expand All @@ -39,5 +39,5 @@
| Level | Problems | Solved | Unsolved |
| --- | --- | --- | --- |
| Easy | 3 | 3 | 0 |
| Medium | 10 | 7 | 3 |
| Medium | 12 | 7 | 5 |
| Hard | 0 | 0 | 0 |

0 comments on commit e9feff5

Please sign in to comment.