Skip to content

Commit e9feff5

Browse files
committed
Dec 15
1 parent 94273d0 commit e9feff5

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from heapq import heapify, heappop, heappush
2+
from typing import List
3+
4+
5+
class Solution:
6+
def maxAverageRatio(
7+
self, classes: List[List[int]], extraStudents: int
8+
) -> float:
9+
def profit(a, b):
10+
return (a + 1) / (b + 1) - a / b
11+
12+
max_heap = []
13+
for a, b in classes:
14+
max_heap.append((-profit(a, b), a, b))
15+
heapify(max_heap)
16+
17+
for _ in range(extraStudents):
18+
_, a, b = heappop(max_heap)
19+
heappush(max_heap, (-profit(a + 1, b + 1), a + 1, b + 1))
20+
21+
return sum(a / b for _, a, b in max_heap) / len(classes)
22+
23+
24+
def main():
25+
classes = [[1, 2], [3, 5], [2, 2]]
26+
extraStudents = 2
27+
result = Solution().maxAverageRatio(classes, extraStudents)
28+
assert abs(result) < 1e-5
29+
30+
classes = [[2, 4], [3, 9], [4, 5], [2, 10]]
31+
extraStudents = 4
32+
result = Solution().maxAverageRatio(classes, extraStudents)
33+
assert abs(result) < 1e-5
34+
35+
36+
if __name__ == '__main__':
37+
main()

2024-12-December-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
| December 12 | [2558. Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/) | Easy | Solved |
1818
| 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 |
1919
| December 14 | [2762. Continuous Subarrays](https://leetcode.com/problems/continuous-subarrays/) | Medium | Unsolved |
20-
| December 15 | []() | | |
20+
| December 15 | [1792. Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/) | Medium | Unsolved |
2121
| December 16 | []() | | |
2222
| December 17 | []() | | |
2323
| December 18 | []() | | |
@@ -39,5 +39,5 @@
3939
| Level | Problems | Solved | Unsolved |
4040
| --- | --- | --- | --- |
4141
| Easy | 3 | 3 | 0 |
42-
| Medium | 10 | 7 | 3 |
42+
| Medium | 12 | 7 | 5 |
4343
| Hard | 0 | 0 | 0 |

0 commit comments

Comments
 (0)