forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
41 lines (38 loc) · 1.27 KB
/
Solution.py
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
class Solution:
def maxCompatibilitySum(self, students: List[List[int]], mentors: List[List[int]]) -> int:
def score(s, m):
res = 0
for i in range(len(s)):
res += 1 if s[i] == m[i] else 0
return res
m, n = len(students), len(students[0])
scores = [[0] * m for _ in range(m)]
for i in range(m):
for j in range(m):
scores[i][j] = score(students[i], mentors[j])
p = self.permute(list(range(m)))
mx = 0
for item in p:
t = 0
sidx = 0
for midx in item:
t += scores[sidx][midx]
sidx += 1
mx = max(mx, t)
return mx
def permute(self, nums):
def dfs(nums, i, res, path, used):
if i == len(nums):
res.append(copy.deepcopy(path))
return
for j in range(len(nums)):
if not used[j]:
path.append(nums[j])
used[j] = True
dfs(nums, i + 1, res, path, used)
used[j] = False
path.pop()
res, path = [], []
used = [False] * len(nums)
dfs(nums, 0, res, path, used)
return res