Maximum Compatibility Score Sum LeetCode solution in Java

1
class Solution {
2
int max;
3

4
public int maxCompatibilitySum(int[][] students, int[][] mentors) {
5
boolean[] visited = new boolean[students.length];
6
helper(visited, students, mentors, 0, 0);
7
return max;
8
}
9

10
public void helper(boolean[] visited, int[][] students, int[][] mentors, int pos, int score) {
11
if (pos >= students.length) {
12
max = Math.max(max, score);
13
return;
14
}
15
for (int i = 0; i < mentors.length; i++)
16
if (!visited[i]) {
17
visited[i] = true;
18
helper(visited, students, mentors, pos + 1, score + score(students[pos], mentors[i]));
19
visited[i] = false;
20
}
21
}
22

23
public int score(int[] a, int[] b) {
24
int count = 0;
25

26
for (int i = 0; i < b.length; i++) if (a[i] == b[i]) count += 1;
27
return count;
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0