File tree 1 file changed +13
-12
lines changed
scripts/algorithms/M/Maximum Compatibility Score Sum
1 file changed +13
-12
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 239 ms (Top 46.15%) | Memory: 42.4 MB (Top 84.62%)
1
2
var maxCompatibilitySum = function ( students , mentors ) {
2
3
const m = students . length ;
3
4
const n = students [ 0 ] . length ;
4
-
5
+
5
6
let max = 0 ;
6
-
7
+
7
8
dfs ( 0 , ( 1 << m ) - 1 , 0 ) ;
8
-
9
+
9
10
return max ;
10
-
11
+
11
12
function dfs ( studentIdx , bitmask , scoreTally ) {
12
13
if ( studentIdx === m ) {
13
14
max = Math . max ( max , scoreTally ) ;
14
-
15
+
15
16
return ;
16
17
}
17
-
18
+
18
19
for ( let mentorIdx = 0 ; mentorIdx < m ; ++ mentorIdx ) {
19
20
if ( bitmask & ( 1 << mentorIdx ) ) {
20
21
const matchScore = hammingDistance ( students [ studentIdx ] , mentors [ mentorIdx ] ) ;
21
22
const setMask = bitmask ^ ( 1 << mentorIdx ) ;
22
-
23
+
23
24
dfs ( studentIdx + 1 , setMask , scoreTally + matchScore ) ;
24
25
}
25
26
}
26
-
27
+
27
28
return ;
28
29
}
29
-
30
+
30
31
function hammingDistance ( studentsAnswers , mentorsAnswers ) {
31
32
let matches = 0 ;
32
-
33
+
33
34
for ( let j = 0 ; j < n ; ++ j ) {
34
35
if ( studentsAnswers [ j ] === mentorsAnswers [ j ] ) ++ matches ;
35
36
}
36
-
37
+
37
38
return matches ;
38
39
}
39
- } ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments