Maximum Average Pass Ratio

A school has `n` classes. `classes[i] = [pass_i, total_i]` means class `i` has `total_i` students, of whom `pass_i` will pass the final exam. You also have `extraStudents` brilliant students who are guaranteed to pass, and each one can be added to any class. The pass ratio of a class is passing students divided by total students, and the average pass ratio is the mean over all classes. Assign every extra student so the average pass ratio is as large as possible, and return it. Return the average rounded to 5 decimal places.

30:00

Maximum Average Pass Ratio
medium
Free written solution
Topics
Companies

A school has n classes. classes[i] = [pass_i, total_i] means class i has total_i students, of whom pass_i will pass the final exam. You also have extraStudents brilliant students who are guaranteed to pass, and each one can be added to any class. The pass ratio of a class is passing students divided by total students, and the average pass ratio is the mean over all classes. Assign every extra student so the average pass ratio is as large as possible, and return it. Return the average rounded to 5 decimal places.

Example 1:
Input: {"classes":[[1,2],[3,5],[2,2]],"extraStudents":2}
Output: 0.78333
Constraints:
  • 1≤classes.length≤1051 \leq \text{classes.length} \leq 10^5

  • classes[i].length=2\text{classes}[i].\text{length} = 2

  • 1≤passi≤totali≤1051 \leq \text{pass}_i \leq \text{total}_i \leq 10^5

  • 1≤extraStudents≤1051 \leq \text{extraStudents} \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"classes":[[1,2],[3,5],[2,2]],"extraStudents":2}

extra students left 2 / 2

average 0.7000

classes: pass / total (green = added students)

#0

1/2

+.1667

#1

3/5

+.0667

#2

2/2

+.0000

right column: gain if one more student joins

max-heap by gain

not built yet

3 classes and 2 extra students. Each extra student raises one class's ratio from p/t to (p+1)/(t+1); we want the biggest raise each time.

Variables
VariableValue
left2
DepthFunction Call
Stack empty
0/7