Skip to content
Decoder

Maximum Average Pass Ratio

Medium

Problem

A school has several classes. classes[i] = [pass_i, total_i] means class i has total_i students, of whom pass_i will pass the exam. You also have extra_students (extraStudents in TypeScript) brilliant students who are guaranteed to pass. You can place each of them into any class.

A class's pass ratio is pass / total. Assign every extra student to maximize the average pass ratio across all classes, and return that average.

Example 1

Input
classes = [[1, 2], [3, 5], [2, 2]], extra_students = 2
Output
0.78333

Both extra students go to the first class: ratios become 3/4, 3/5, 2/2, averaging 0.78333. Answers within 10^-5 of the true value are accepted.

Constraints

  • 1 <= classes.length <= 10^5
  • 1 <= pass_i <= total_i <= 10^5
  • 1 <= extra_students <= 10^5

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.