Skip to content

Commit a65c908

Browse files
authored
Create 1947-maximum-compatibility-score-sum.js
1 parent b33939c commit a65c908

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[][]} students
3+
* @param {number[][]} mentors
4+
* @return {number}
5+
*/
6+
const maxCompatibilitySum = function(students, mentors) {
7+
const obj = { res: 0 }, hash = {}
8+
for(let i = 0, n = students.length; i < n; i++) {
9+
bt(students, mentors, 0, 0, obj, hash)
10+
}
11+
return obj.res
12+
};
13+
14+
function bt(stu, men, i, score, obj, hash) {
15+
16+
if(i === stu.length) {
17+
if(score > obj.res) {
18+
obj.res = score
19+
// console.log(hash)
20+
}
21+
return
22+
}
23+
24+
for(let j = 0; j < men.length; j++) {
25+
const k = `${j}`
26+
if(hash[k] === 1) continue
27+
hash[k] = 1
28+
bt(stu, men, i + 1, score + calc(stu[i], men[j]), obj, hash)
29+
delete hash[k]
30+
}
31+
}
32+
33+
function calc(a1, a2) {
34+
const n = a1.length
35+
let res = 0
36+
for(let i = 0; i < n; i++) {
37+
if(a1[i] === a2[i]) res++
38+
}
39+
return res
40+
}

0 commit comments

Comments
 (0)