Ctrl K

Top Students to Lift the Groups

Medium
A school has several study groups, each with an exam coming up. For group i you know groups[i] = [p, t]: it has t students, and exactly p of them will pass. The group's pass rate is p / t. There are also helpers new top students, each certain to pass in whichever group they join (adding 1 to both the group's size and its number of passers). Assign every one of them to a group so that the average of the pass rates across all groups is as large as possible, and return that average. Answers within 10⁻⁵ of the exact value are accepted.
Example 1
groups=[[1, 3], [4, 4]], helpers=1 → 0.75

Everyone in the second group already passes, so the helper joins the first one: (2/4 + 4/4) / 2 = 0.75.

Example 2
groups=[[2, 5], [1, 2]], helpers=2 → 0.5833333333333333

One helper per group is best: (3/6 + 2/3) / 2 ≈ 0.58333. Both in the first group give ≈ 0.53571, both in the second give 0.575.

Constraints
  • 1 ≤ len(groups) ≤ 10⁵
  • groups[i] = [p, t], 1 ≤ p ≤ t ≤ 10⁵
  • 1 ≤ helpers ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumJobs With a Cooldown MediumSorted Into Runs of Three MediumSpread Out the Labels MediumSplicing Ropes Medium
loading Python…
Input
groups=[[1, 3], [4, 4]], helpers=1
Expected
0.75

Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.