Leetcode 1792: Maximum Average Pass Ratio

grid47
grid47
Exploring patterns and algorithms
May 11, 2024 7 min read

A school has several classes, and each class will have students taking a final exam. You are provided with a 2D array where each class is represented by two values: the number of students who will pass and the total number of students in the class. Additionally, a number of brilliant students, ’extraStudents,’ is provided. These brilliant students are guaranteed to pass if assigned to any class. The goal is to assign these extra students to maximize the average pass ratio of all classes. The pass ratio of a class is defined as the number of students passing divided by the total number of students in the class. You need to return the maximum possible average pass ratio after optimally distributing the extra students.
Problem
Approach
Steps
Complexity
Input: You are given a 2D array classes where each element classes[i] = [passi, totali], representing the number of students passing and the total number of students in the ith class. You are also given an integer extraStudents representing the number of extra students who can be assigned to any class to help improve the pass ratio.
Example: Input: classes = [[2, 5], [3, 6], [4, 8]], extraStudents = 3
Constraints:
• 1 <= classes.length <= 10^5
• classes[i].length == 2
• 1 <= passi <= totali <= 10^5
• 1 <= extraStudents <= 10^5
Output: Return the maximum possible average pass ratio after optimally assigning the extra students.
Example: Output: 0.72345
Constraints:
Goal: Maximize the average pass ratio by optimally assigning extra students to classes.
Steps:
• Calculate the initial pass ratio for each class.
• For each class, compute the potential gain in the pass ratio if one extra student is assigned.
• Use a priority queue to always assign extra students to the class with the highest potential gain in pass ratio.
• Repeat the process until all extra students are assigned.
• Return the average of the new pass ratios across all classes.
Goal: The input will always follow the specified constraints, ensuring the validity of the graph.
Steps:
• The number of classes will not exceed 10^5.
• Each class has at least one student and the total number of students in any class is at most 10^5.
• The number of extra students will be between 1 and 10^5.
Assumptions:
• All classes have a valid number of students, and the input data follows the format specified.
Input: classes = [[2, 5], [3, 6], [4, 8]], extraStudents = 3
Explanation: In this case, we can assign the extra students to the classes with the lowest initial pass ratios to maximize the average pass ratio.

Input: classes = [[1, 2], [3, 5], [2, 2]], extraStudents = 2
Explanation: Assigning the extra students to the first class will result in the highest increase in pass ratio.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus