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
The compatibility score between a student and mentor is simply the number of questions they answered the same way. For example:
โข Student answers:
โข Mentor answers:
โข 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.
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.
Visualization
Tap to expand
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.
Time & Space Complexity
Time Complexity
O(mยฒ ร 2^m)
For each of 2^m possible mentor assignments, we try m mentors for m students
โ Linear Growth
Space Complexity
O(m ร 2^m)
Memoization table stores results for each (student, mentor_mask) pair
โ Linear Space
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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code