3-line Python
An alternative optimization is to decorate function score() with @cache to avoid duplicated computations.\n\npython\n    def maxCompatibilitySum(self, students:
An alternative optimization is to decorate function `score()` with `@cache` to avoid duplicated computations.\n\n```python\n    def maxCompatibilitySum(self, students: List[List[int]], mentors: List[List[int]]) -> int:\n        def score(s_id: int, m_id: int) -> int:\n            return sum(map(lambda x, y: 1^x^y, students[s_id], mentors[m_id]))\n        return max(sum(score(i, j) for i, j in enumerate(p)) for p in permutations(range(len(students))))\n```
[]