15% off all plans & roadmaps Use code
See plansLeetCode 1792 · Solution & editorial
There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array classes, where classes[i] = [passi, totali]. You know beforehand that in the ith class, there are totali total students, but only passi number of students will pass the exam.
Maximum Average Pass Ratio is a Heap (Priority Queue) Patterns problem, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern. The pattern covers top K elements, K-way merge, two heaps for median, and scheduling
Solve scheduling and optimization problems using priority queue with greedy approach.
The reference solution runs in O((n + extraStudents) * log n) time and O(n) space.
Maximum Average Pass Ratio has been reported in interviews at Amazon.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Average Pass Ratio is a Heap (Priority Queue) Patterns problem, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.