Ctrl K

Trainees and Coaches

Medium
A questionnaire has n yes/no questions, each answered with 1 (yes) or 0 (no). It was filled in by m trainees and m coaches: trainees[i] holds the answers of trainee i, and coaches[j] the answers of coach j. The compatibility of a trainee–coach pair is the number of questions they answered the same way. Each trainee must be assigned to exactly one coach so that every coach also ends up with exactly one trainee. Return the largest possible total compatibility over all pairs.
Example 1
trainees=[[1, 0, 1], [0, 1, 1]], coaches=[[0, 1, 0], [1, 0, 1]] → 5

Pair the first trainee with the second coach (all 3 answers match) and the second trainee with the first coach (2 answers match): 3 + 2 = 5. The other assignment gives 0 + 1 = 1.

Example 2
trainees=[[1, 1]], coaches=[[0, 1]] → 1

There is only one pair, and only the second answer matches.

Example 3
trainees=[[1, 0], [1, 0], [0, 0]], coaches=[[1, 0], [0, 1], [1, 1]] → 4

The first coach suits both of the first two trainees (2 matches each) but can take only one. The best plan: trainee 1 to coach 1 (2), trainee 2 to coach 3 (1), trainee 3 to coach 2 (1), 4 in total.

Constraints
  • m = trainees.length = coaches.length
  • n = the length of every row of trainees and coaches
  • 1 ≤ m, n ≤ 8
  • every answer is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSquare Frame From Sticks MediumSeating by Divisibility MediumCheapest Basket with Bundles MediumGroups With Equal Sums MediumLeast Total Walk to Scooters Medium
loading Python…
Input
trainees=[[1, 0, 1], [0, 1, 1]], coaches=[[0, 1, 0], [1, 0, 1]]
Expected
5

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