Skip to content

Commit b7a8d17

Browse files
committed
Leetcode | Maximum Compatibility Score Sum | Kotlin
1 parent b36e404 commit b7a8d17

3 files changed

Lines changed: 118 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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.devstromo.medium.p1947
2+
3+
class SolutionKt {
4+
fun maxCompatibilitySum(students: Array<IntArray>, mentors: Array<IntArray>): Int {
5+
val m = students.size
6+
val n = students[0].size
7+
8+
// Precompute compatibility scores
9+
val score = Array(m) { IntArray(m) }
10+
for (i in 0 until m) {
11+
for (j in 0 until m) {
12+
var match = 0
13+
for (k in 0 until n) {
14+
if (students[i][k] == mentors[j][k]) {
15+
match++
16+
}
17+
}
18+
score[i][j] = match
19+
}
20+
}
21+
22+
val dp = IntArray(1 shl m)
23+
24+
for (mask in 0 until (1 shl m)) {
25+
val studentIndex = mask.countOneBits()
26+
if (studentIndex >= m) continue
27+
28+
for (j in 0 until m) {
29+
if ((mask and (1 shl j)) == 0) { // mentor j not yet assigned
30+
val nextMask = mask or (1 shl j)
31+
dp[nextMask] = maxOf(dp[nextMask], dp[mask] + score[studentIndex][j])
32+
}
33+
}
34+
}
35+
36+
return dp[(1 shl m) - 1]
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.devstromo.medium.p1947
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.DisplayName
5+
import org.junit.jupiter.api.Test
6+
7+
class SolutionKtUnitTest {
8+
private val solution = SolutionKt()
9+
10+
@Test
11+
@DisplayName("Test Maximum Compatibility Score Sum")
12+
fun testMaximumCompatibilityScoreSum() {
13+
assertEquals(
14+
8, solution.maxCompatibilitySum(
15+
arrayOf(
16+
intArrayOf(1, 1, 0),
17+
intArrayOf(1, 0, 1),
18+
intArrayOf(0, 0, 1)
19+
),
20+
arrayOf(
21+
intArrayOf(1, 0, 0),
22+
intArrayOf(0, 0, 1),
23+
intArrayOf(1, 1, 0)
24+
)
25+
)
26+
)
27+
assertEquals(
28+
0, solution.maxCompatibilitySum(
29+
arrayOf(
30+
intArrayOf(0, 0),
31+
intArrayOf(0, 0),
32+
intArrayOf(0, 0),
33+
),
34+
arrayOf(
35+
intArrayOf(1, 1),
36+
intArrayOf(1, 1),
37+
intArrayOf(1, 1),
38+
)
39+
)
40+
)
41+
}
42+
}

0 commit comments

Comments
 (0)