1792. Maximum Average Pass Ratio
Maximum Average Pass Ratio (LeetCode 1792) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Heap (Priority Queue) Patterns pattern, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern.
How to solve Maximum Average Pass Ratio
Maximum Average Pass Ratio is a Scheduling and Minimum Cost problem, part of Heap (Priority Queue) Patterns. Solve scheduling and optimization problems using priority queue with greedy approach.
Work towards the solution in these steps:
- Pay attention to how much the pass ratio changes when you add a student to the class. If you keep adding students, what happens to the change in pass ratio? The more students you add to a class, the smaller the change in pass ratio becomes.
- Since the change in the pass ratio is always decreasing with the more students you add, then the very first student you add to each class is the one that makes the biggest change in the pass ratio.
- Because each class's pass ratio is weighted equally, it's always optimal to put the student in the class that makes the biggest change among all the other classes.
The reference solution runs in O((n + extraStudents) * log n) time and O(n) space.
The Maximum Average Pass Ratio solution and editorial walks through the full approach with reference code.
Problem statement
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.
You are also given an integer extraStudents. There are another extraStudents brilliant students that are guaranteed to pass the exam of any class they are assigned to. You want to assign each of the extraStudents students to a class in a way that maximizes the average pass ratio across all the classes.
The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes.
Return the maximum possible average pass ratio after assigning the extraStudents students. Answers within 10-5 of the actual answer will be accepted.
Example 1:
Input: classes = [[1,2],[3,5],[2,2]], extraStudents = 2 Output: 0.78333 Explanation: You can assign the two extra students to the first class. The average pass ratio will be equal to (3/4 + 3/5 + 2/2) / 3 = 0.78333.
Example 2:
Input: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4 Output: 0.53485
Constraints:
- 1 <= classes.length <= 105
- classes[i].length == 2
- 1 <= passi <= totali <= 105
- 1 <= extraStudents <= 105
Topics and companies
Topics: Array, Greedy, Heap (Priority Queue).
Reported in interviews at Amazon.
How to practise Maximum Average Pass Ratio on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O((n + extraStudents) * log n) time and O(n) space.
Open Maximum Average Pass Ratio in the code editor, or read the Maximum Average Pass Ratio editorial for a worked solution with its approach and complexity analysis.
Where Maximum Average Pass Ratio sits in the DSA pattern sheet
- Heap (Priority Queue) Patterns pattern guide
- Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern
- The full DSA patterns sheet
Problems related to Maximum Average Pass Ratio
Other problems that use the same Scheduling / Minimum Cost (Greedy with Priority Queue) and Heap (Priority Queue) Patterns techniques:
- Furthest Building You Can Reach — Medium, same subpattern
- IPO — Hard, same subpattern
- Longest Happy String — Medium, same subpattern
- Meeting Rooms II — Medium, same subpattern
- Meeting Rooms III — Hard, same subpattern
- Minimum Cost to Hire K Workers — Hard, same subpattern
Maximum Average Pass Ratio FAQ
What is the LeetCode number of Maximum Average Pass Ratio?
Maximum Average Pass Ratio is LeetCode problem 1792, rated Medium.
What pattern is used to solve Maximum Average Pass Ratio?
Maximum Average Pass Ratio is a Scheduling and Minimum Cost problem. It belongs to the Heap (Priority Queue) Patterns pattern, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern.
What is the time complexity of Maximum Average Pass Ratio?
The reference solution runs in O((n + extraStudents) * log n) time and O(n) space.
Which companies ask Maximum Average Pass Ratio?
Maximum Average Pass Ratio has been reported in interviews at Amazon.