-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.ts
30 lines (30 loc) · 893 Bytes
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function maxCompatibilitySum(students: number[][], mentors: number[][]): number {
let ans = 0;
const m = students.length;
const vis: boolean[] = Array(m).fill(false);
const g: number[][] = Array.from({ length: m }, () => Array(m).fill(0));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < m; ++j) {
for (let k = 0; k < students[i].length; ++k) {
if (students[i][k] === mentors[j][k]) {
g[i][j]++;
}
}
}
}
const dfs = (i: number, s: number): void => {
if (i >= m) {
ans = Math.max(ans, s);
return;
}
for (let j = 0; j < m; ++j) {
if (!vis[j]) {
vis[j] = true;
dfs(i + 1, s + g[i][j]);
vis[j] = false;
}
}
};
dfs(0, 0);
return ans;
}