Bitmask DP - [33%, 6%]
Complexity\nTime complexity: $O(2^n + 2^{n/k})$\nSpace complexity: $O(2^n + 2^{n/k})$\n\n# Code\npython\nclass Solution:\n    def minimumIncompatibility(self, A
# Complexity\nTime complexity: $O(2^n + 2^{n/k})$\nSpace complexity: $O(2^n + 2^{n/k})$\n\n# Code\n```python\nclass Solution:\n    def minimumIncompatibility(self, A: List[int], K: int) -> int:\n        S = len(A) // K\n        # pre-calc the bitmasks\n        M = reduce(lambda M, i: {\n                n | (1 << i) for i in range(len(A)) \n                for n in M if (n >> i) & 1 == 0 \n                and all((n >> j) & 1 == 0 or A[j] != A[i] for j in range(len(A)))\n            },\n            range(S),\n            {0}\n        )\n\n        # find \'incompatability\' for each mask \n        D = {\n            m: max(A[i] for i in range(len(A)) if (m >> i) & 1)\\\n                - min(A[i] for i in range(len(A)) if (m >> i) & 1)\\\n                for m in M\n        }\n        \n        @cache\n        def f(m: int) -> int:\n            if m == (1 << len(A)) - 1: return 0\n            return min(\n                (f(m | nm) + D[nm] for nm in M if not (nm & m)), default=maxsize\n            )\n\n        return f(0) if f(0) < maxsize else -1\n        \n```
['Dynamic Programming', 'Bitmask', 'Python3']