Leetcode 1947: Maximum Compatibility Score Sum

grid47
grid47
Exploring patterns and algorithms
Apr 26, 2024 6 min read

You are given a survey with n questions where each question’s answer is either 0 (no) or 1 (yes). The survey is completed by m students and m mentors. Each student’s answers are represented by a 2D array of size m x n, and similarly, each mentor’s answers are represented by another 2D array. The compatibility score of a student-mentor pair is calculated as the number of answers that are the same for both the student and the mentor. Your task is to find the optimal pairing of students to mentors that maximizes the sum of the compatibility scores.
Problem
Approach
Steps
Complexity
Input: You are given two 2D arrays: one representing the students' answers and another representing the mentors' answers. Both arrays have m rows (one for each student and mentor) and n columns (one for each question's answer). Each entry in the arrays is either 0 or 1.
Example: students = [[1,1,0],[1,0,1],[0,0,1]], mentors = [[1,0,0],[0,0,1],[1,1,0]]
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
Output: Return the maximum sum of compatibility scores that can be achieved by optimally pairing students to mentors.
Example: Output: 8
Constraints:
• The output should be a single integer representing the maximum compatibility score sum.
Goal: The goal is to find the optimal pairings between students and mentors that maximize the sum of compatibility scores.
Steps:
• Step 1: For each student-mentor pairing, calculate the compatibility score by counting the number of matching answers.
• Step 2: Find the optimal pairing by checking all possible permutations of student-mentor assignments.
• Step 3: Sum the compatibility scores of the optimal pairings and return the result.
Goal: The problem involves finding the best student-mentor pairings using the given constraints on input size and values.
Steps:
• 1 <= m, n <= 8
• students and mentors contain only 0 or 1 values.
Assumptions:
• The input sizes are small enough (maximum 8 students/mentors and 8 questions) that checking all permutations is computationally feasible.
• Each student will be assigned to exactly one mentor, and vice versa.
Input: Input: students = [[1,1,0],[1,0,1],[0,0,1]], mentors = [[1,0,0],[0,0,1],[1,1,0]]
Explanation: The possible pairings are evaluated to find the one with the highest sum of compatibility scores. The optimal pairings in this case are student 0 with mentor 2 (compatibility score 3), student 1 with mentor 0 (score 2), and student 2 with mentor 1 (score 3). The total score is 8.

Input: Input: students = [[0,0],[0,0],[0,0]], mentors = [[1,1],[1,1],[1,1]]
Explanation: In this case, all compatibility scores are 0 because no student-answer matches any mentor-answer, so the total score is 0.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus