Maximum Average Pass Ratio - Problem

Imagine you're an academic advisor at a prestigious school with the power to strategically place brilliant students to maximize the overall success rate across all classes! ๐ŸŽ“

You have classes, where each class i currently has passi students who will pass out of totali total students. You also have extraStudents brilliant students who are guaranteed to pass any class they're assigned to.

Your mission: Assign these extra students to maximize the average pass ratio across all classes.

The pass ratio of a class = passing_students / total_students
The average pass ratio = sum of all pass ratios / number of classes

Key Insight: Adding a student to a class with a lower current ratio typically yields a bigger improvement than adding to a class with a higher ratio!

Return the maximum possible average pass ratio after optimally assigning all extra students.

Input & Output

example_1.py โ€” Basic Case
$ Input: classes = [[1,2],[3,5],[2,2]], extraStudents = 2
โ€บ Output: 0.78333
๐Ÿ’ก Note: Initially: 1/2=0.5, 3/5=0.6, 2/2=1.0. Adding students optimally: first to [1,2]โ†’[2,3] (biggest improvement), then to [2,3]โ†’[3,4]. Final: 3/4 + 3/5 + 2/2 = 2.35, average = 2.35/3 โ‰ˆ 0.78333
example_2.py โ€” Single Class
$ Input: classes = [[2,4]], extraStudents = 1
โ€บ Output: 0.75000
๐Ÿ’ก Note: Only one class [2,4] with ratio 2/4=0.5. Adding one student gives [3,5] with ratio 3/5=0.6. Since there's only one class, average equals this ratio: 0.6
example_3.py โ€” No Improvement Needed
$ Input: classes = [[1,1],[2,2]], extraStudents = 2
โ€บ Output: 1.00000
๐Ÿ’ก Note: Both classes already have 100% pass rate. Adding students doesn't improve ratios: [2,2] and [3,3] both have ratio 1.0. Average remains 1.0

Constraints

  • 1 โ‰ค classes.length โ‰ค 105
  • classes[i].length == 2
  • 1 โ‰ค passi โ‰ค totali โ‰ค 105
  • 1 โ‰ค extraStudents โ‰ค 105
  • Answers within 10-5 of the actual answer will be accepted

Visualization

Tap to expand
Strategic Student Placement VisualizationClass A1 pass / 2 totalRatio: 50%0.167ImprovementClass B3 pass / 5 totalRatio: 60%0.067ImprovementClass C2 pass / 2 totalRatio: 100%0.0ImprovementPriority Queue1st: Class A (0.167)2nd: Class B (0.067)3rd: Class C (0.0)Always pick max!Student 1After First AssignmentClass A: 2/3 (66.7%) - New improvement: 0.083Class B: 3/5 (60%) - Improvement: 0.067Class C: 2/2 (100%) - Improvement: 0.0Key Mathematical InsightImprovement Formula: (passes + 1)/(total + 1) - passes/totalThis measures marginal gain in pass ratio from adding one studentLower current ratios typically have higher improvement potentialPerfect classes (100% pass rate) have zero improvement potentialTime ComplexityO(k log n) where k=extra students
Understanding the Visualization
1
Calculate ROI
For each class, calculate the 'return on investment' - how much the pass ratio improves by adding one student
2
Invest Wisely
Always invest in the opportunity with the highest ROI (use a max heap to track this efficiently)
3
Update Opportunities
After each investment, recalculate the ROI for that class and update your priorities
4
Repeat
Continue until all your resources (extra students) are allocated optimally
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because improvement potential decreases as pass ratios increase. By always choosing the class with maximum improvement potential, we guarantee the optimal allocation of extra students to maximize the overall average pass ratio!
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
89.3K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen