forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
63 lines (59 loc) · 1.7 KB
/
Solution.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
public int maxCompatibilitySum(int[][] students, int[][] mentors) {
int m = students.length, n = students[0].length;
int[][] scores = new int[m][m];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
scores[i][j] = score(students[i], mentors[j]);
}
}
int[] idx = new int[m];
for (int i = 0; i < m; ++i) {
idx[i] = i;
}
int mx = 0;
List<List<Integer>> p = permute(idx);
for (List<Integer> item : p) {
int t = 0;
int sidx = 0;
for (int midx : item) {
t += scores[sidx][midx];
++sidx;
}
mx = Math.max(mx, t);
}
return mx;
}
private int score(int[] s, int[] m) {
int res = 0;
for (int i = 0; i < s.length; ++i) {
res += s[i] == m[i] ? 1 : 0;
}
return res;
}
private List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
permute(res, nums, 0);
return res;
}
private void permute(List<List<Integer>> res, int[] nums, int start) {
if (start == nums.length) {
List<Integer> t = new ArrayList<>();
for (int e : nums) {
t.add(e);
}
res.add(t);
return;
}
for (int i = start; i < nums.length; ++i) {
swap(nums, i, start);
permute(res, nums, start + 1);
swap(nums, i, start);
}
}
private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}