Maximum Compatibility Score Sum LeetCode solution in JavaScript

1
var maxCompatibilitySum = function (students, mentors) {
2
const m = students.length;
3
const n = students[0].length;
4

5
let max = 0;
6

7
dfs(0, (1 << m) - 1, 0);
8

9
return max;
10

11
function dfs(studentIdx, bitmask, scoreTally) {
12
if (studentIdx === m) {
13
max = Math.max(max, scoreTally);
14

15
return;
16
}
17

18
for (let mentorIdx = 0; mentorIdx < m; ++mentorIdx) {
19
if (bitmask & (1 << mentorIdx)) {
20
const matchScore = hammingDistance(
21
students[studentIdx],
22
mentors[mentorIdx]
23
);
24
const setMask = bitmask ^ (1 << mentorIdx);
25

26
dfs(studentIdx + 1, setMask, scoreTally + matchScore);
27
}
28
}
29

30
return;
31
}
32

33
function hammingDistance(studentsAnswers, mentorsAnswers) {
34
let matches = 0;
35

36
for (let j = 0; j < n; ++j) {
37
if (studentsAnswers[j] === mentorsAnswers[j]) ++matches;
38
}
39

40
return matches;
41
}
42
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0