Maximum Compatibility Score Sum - Problem
Maximum Compatibility Score Sum

Imagine you're running a mentorship program where you need to pair students with mentors based on their survey responses! ๐ŸŽ“

You have m students and m mentors who each completed a survey with n binary questions (answered with 0 or 1). Your goal is to create perfect one-to-one pairings that maximize the total compatibility.

The compatibility score between a student and mentor is simply the number of questions they answered the same way. For example:
โ€ข Student answers: [1, 0, 1]
โ€ข Mentor answers: [0, 0, 1]
โ€ข Compatibility score: 2 (positions 1 and 2 match)

Since each student must be paired with exactly one mentor (and vice versa), you need to find the optimal assignment that maximizes the sum of all compatibility scores.

Input & Output

example_1.py โ€” Basic Case
$ Input: students = [[1,1,0],[1,0,1],[0,0,1]], mentors = [[1,0,0],[0,0,1],[1,1,0]]
โ€บ Output: 8
๐Ÿ’ก Note: Optimal pairing: Student 0 โ†” Mentor 2 (score 2), Student 1 โ†” Mentor 0 (score 2), Student 2 โ†” Mentor 1 (score 4). Total: 2+2+4 = 8
example_2.py โ€” Perfect Match
$ Input: students = [[0,0],[0,0],[0,0]], mentors = [[1,1],[0,1],[1,0]]
โ€บ Output: 0
๐Ÿ’ก Note: All students answered [0,0] but no mentor has the same answers. Best we can do is 0 matches per pair, total = 0.
example_3.py โ€” Single Pair
$ Input: students = [[1,0,1]], mentors = [[0,1,1]]
โ€บ Output: 1
๐Ÿ’ก Note: Only one student and one mentor. They match on position 2, so compatibility score = 1.

Constraints

  • m == students.length == mentors.length
  • n == students[i].length == mentors[j].length
  • 1 โ‰ค m, n โ‰ค 8
  • students[i][k] is either 0 or 1
  • mentors[j][k] is either 0 or 1

Visualization

Tap to expand
๐ŸŽ“ Student-Mentor Compatibility MatchingCompatibility Matrix๐Ÿ“š Students๐Ÿ‘จโ€๐Ÿซ Mentors212211130S0S1S2M0M1M2๐ŸŽฏ Optimal AssignmentS0 โ†’ M2Score: 2S1 โ†’ M0Score: 2S2 โ†’ M1Score: 3Total Score: 2+2+3 = 7๐Ÿš€ Algorithm Steps1Calculate all pairwise compatibility scores2Use bitmask DP: dp(student_idx, mentor_mask)3Try assigning current student to each available mentor4Memoize results and return maximum score
Understanding the Visualization
1
Build Compatibility Matrix
Calculate how many survey answers match between each student-mentor pair
2
Generate All Assignments
Use bitmask to represent which mentors are taken and try all possibilities
3
Memoize Subproblems
Cache results for each state to avoid recalculating same scenarios
4
Return Maximum
Find the assignment that gives the highest total compatibility score
Key Takeaway
๐ŸŽฏ Key Insight: This assignment problem can be solved optimally using bitmask dynamic programming in O(mยฒ ร— 2^m) time, which is much more efficient than trying all m! permutations. The bitmask elegantly tracks which mentors are available while memoization prevents redundant calculations.
Asked in
Google 28 Amazon 22 Meta 18 Microsoft 15
32.4K Views
Medium Frequency
~25 min Avg. Time
892 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