[Python] 104ms beats 100%
\nfrom collections import Counter\nfrom itertools import combinations\nclass Solution:\n    def minimumIncompatibility(self, nums: List[int], k: int) -> int:\n 
```\nfrom collections import Counter\nfrom itertools import combinations\nclass Solution:\n    def minimumIncompatibility(self, nums: List[int], k: int) -> int:\n        Ctmp = Counter(nums)\n        C = [0]*(max(v for v in Ctmp)+1)\n        for v in Ctmp:\n            C[v] = Ctmp[v]\n        \n        \n        @lru_cache(None) # I saw this trick from other answers\n        def dfs(C,k):\n            if k==0:\n                return 0\n            V = [v for v,c in enumerate(C) if c>0]\n            tot = sum(C)\n            if max(C)>k:\n                return None\n\n            dev = float(\'inf\')\n\t\t\t# In the following for loop I only count potential \n\t\t\t# groups that includes the smallest element\n            for g in combinations(V[1:], tot//k-1):\n                g = [V[0]]+list(g)\n                newC = list(C)\n                for v in g:\n                    newC[v] -=1\n                o = dfs(tuple(newC), k-1)\n                if o is not None:\n                    dev = min(dev, g[-1]-g[0]+o)\n\n            if dev<float(\'inf\'):\n                return int(dev)\n            return None\n        \n        out = dfs(tuple(C), k)\n        if out is not None:\n            return out\n        return -1\n```
[]