[Python] backtracking, dfs with pruning
\nclass Solution:\n    def minimumIncompatibility(self, nums: List[int], k: int) -> int:\n        n = len(nums)\n        subsets = tuple([set()] * k)\n        s
```\nclass Solution:\n    def minimumIncompatibility(self, nums: List[int], k: int) -> int:\n        n = len(nums)\n        subsets = tuple([set()] * k)\n        set_size = n // k\n        acc = float(\'inf\')\n        \n        def recur(i: int, subsets: "Tuple[Set(int)]") -> int:\n            res = float(\'inf\')\n            for j in range(k):\n                if nums[i] not in subsets[j] and len(subsets[j]) < set_size:\n                    visited = False\n                    for x in range(j):\n                        if subsets[x] == subsets[j]:\n                            visited = True\n                    if not visited: # pruning: if the same subset has been considered, stop going forward\n                        new = [s.copy() for s in subsets]\n                        new[j].add(nums[i])\n                        curr = check(new)\n                        if i == n - 1:\n                            res = min(res, curr)\n                        else:\n                            nonlocal acc\n                            if curr < acc: # pruing: if the current sum of incompatibilities is already bigger than current best result, stop going forward\n                                res = min(res, recur(i+1, new))\n            if res < acc:\n                acc = res\n            return res\n        \n        def check(subsets: "Tuple[Set(int)]") -> int:\n            return sum([max(s) - min(s) if s else 0 for s in subsets])\n            \n        \n        ans = recur(0, subsets)\n        if ans == float(\'inf\'):\n            ans  = -1\n        return ans\n                \n```
[]