[Python 3] Easy understand Backtracking Solution
The first thing we need to do is to initialize the student-teacher compatibility score matrix:\nmat[i][j] = n - sum(abs(students[i][x] - mentors[j][x]) for x in
The first thing we need to do is to initialize the `student-teacher` compatibility score matrix:\n`mat[i][j] = n - sum(abs(students[i][x] - mentors[j][x]) for x in range(n))`\n\nAfter we build the compatibility score matrix, than it becomes the standard backtracking problem.\nCode:\n```python\nclass Solution:\n    def maxCompatibilitySum(self, students: List[List[int]], mentors: List[List[int]]) -> int:\n        m, n = len(students), len(students[0])\n\t\t# mat[i][j] represents compatibility Score between students i and mentor j\n        mat = [[0] * m for _ in range(m)]\n        \n        for i in range(m):\n            for j in range(m):\n                mat[i][j] = n - sum(abs(students[i][x] - mentors[j][x]) for x in range(n))\n                \n        self.res = 0\n        seen = set()\n        \n        def backtrack(step, score):\n            if step == m:\n                self.res = max(self.res, score)\n                return\n            \n            for i in range(m):\n                if i not in seen:\n                    # pair students and mentor\n\t\t\t\t\t# add mentor\'s index to seen set\n                    seen.add(i)\n                    # move to next state\n                    backtrack(step + 1, score + mat[step][i])\n                    # backtrack\n                    seen.remove(i)\n                    \n        backtrack(0, 0)\n        return self.res\n\n```
[]