Skip to content

Commit 35f87d1

Browse files
committed
Leetcode | Maximum Compatibility Score Sum | Java
1 parent 44915d2 commit 35f87d1

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 1947. Maximum Compatibility Score Sum
2+
3+
There is a survey that consists of `n` questions where each question's answer is either `0` (no) or `1` (yes).
4+
5+
The survey was given to `m` students numbered from `0` to `m - 1` and `m` mentors numbered from `0` to `m - 1`. The answers of the students are represented by a 2D integer array `students` where `students[i]` is an integer array that contains the answers of the `ith` student (0-indexed). The answers of the mentors are represented by a 2D integer array `mentors` where `mentors[j]` is an integer array that contains the answers of the `jth` mentor (0-indexed).
6+
7+
Each student will be assigned to one mentor, and each mentor will have one student assigned to them. The compatibility score of a student-mentor pair is the number of answers that are the same for both the student and the mentor.
8+
9+
- For example, if the student's answers were `[1, 0, 1]` and the mentor's answers were `[0, 0, 1]`, then their compatibility score is 2 because only the second and the third answers are the same.
10+
11+
You are tasked with finding the optimal student-mentor pairings to maximize the sum of the compatibility scores.
12+
13+
Given `students` and `mentors`, return the maximum compatibility score sum that can be achieved.
14+
15+
Example 1:
16+
17+
Input: students = [[1,1,0],[1,0,1],[0,0,1]], mentors = [[1,0,0],[0,0,1],[1,1,0]]
18+
Output: 8
19+
Explanation: We assign students to mentors in the following way:
20+
21+
- student 0 to mentor 2 with a compatibility score of 3.
22+
- student 1 to mentor 0 with a compatibility score of 2.
23+
- student 2 to mentor 1 with a compatibility score of 3.
24+
The compatibility score sum is 3 + 2 + 3 = 8.
25+
26+
Example 2:
27+
28+
Input: students = [[0,0],[0,0],[0,0]], mentors = [[1,1],[1,1],[1,1]]
29+
Output: 0
30+
Explanation: The compatibility score of any student-mentor pair is 0.
31+
32+
Constraints:
33+
34+
- `m == students.length == mentors.length`
35+
- `n == students[i].length == mentors[j].length`
36+
- `1 <= m, n <= 8`
37+
- `students[i][k]` is either `0` or `1`.
38+
- `mentors[j][k]` is either `0` or `1`.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.devstromo.medium.p1947;
2+
3+
public class Solution {
4+
public int maxCompatibilitySum(int[][] students, int[][] mentors) {
5+
int m = students.length;
6+
int n = students[0].length;
7+
8+
// Precompute compatibility scores
9+
int[][] score = new int[m][m];
10+
for (int i = 0; i < m; i++) {
11+
for (int j = 0; j < m; j++) {
12+
int match = 0;
13+
for (int k = 0; k < n; k++) {
14+
if (students[i][k] == mentors[j][k]) {
15+
match++;
16+
}
17+
}
18+
score[i][j] = match;
19+
}
20+
}
21+
22+
// dp[mask]: max score with mentors assigned as in mask
23+
int[] dp = new int[1 << m];
24+
25+
for (int mask = 0; mask < (1 << m); mask++) {
26+
int studentIndex = Integer.bitCount(mask); // how many students have been assigned
27+
if (studentIndex >= m) continue;
28+
29+
for (int j = 0; j < m; j++) {
30+
if ((mask & (1 << j)) == 0) { // mentor j not yet assigned
31+
int nextMask = mask | (1 << j);
32+
dp[nextMask] = Math.max(dp[nextMask], dp[mask] + score[studentIndex][j]);
33+
}
34+
}
35+
}
36+
37+
return dp[(1 << m) - 1];
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.devstromo.medium.p1947;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class SolutionUnitTest {
9+
private final Solution solution = new Solution();
10+
11+
@Test
12+
@DisplayName("Test Maximum Compatibility Score Sum")
13+
void testMaximumCompatibilityScoreSum() {
14+
assertEquals(8, solution.maxCompatibilitySum(
15+
new int[][]{
16+
{1, 1, 0},
17+
{1, 0, 1},
18+
{0, 0, 1}
19+
},
20+
new int[][]{
21+
{1, 0, 0},
22+
{0, 0, 1},
23+
{1, 1, 0}
24+
}
25+
));
26+
assertEquals(0, solution.maxCompatibilitySum(
27+
new int[][]{
28+
{0, 0},
29+
{0, 0},
30+
{0, 0},
31+
},
32+
new int[][]{
33+
{1, 1},
34+
{1, 1},
35+
{1, 1},
36+
}
37+
));
38+
}
39+
}

0 commit comments

Comments
 (0)