[Python] Bottom-Up solution
\nclass Solution:\n    def minimumIncompatibility(self, nums: List[int], k: int) -> int:\n        def countBit(n):\n            res = 0\n            while n != 
```\nclass Solution:\n    def minimumIncompatibility(self, nums: List[int], k: int) -> int:\n        def countBit(n):\n            res = 0\n            while n != 0:\n                res += n % 2\n                n = n // 2\n            return res\n        def toNum(l):\n            return sum([1 << i for i in l])\n        def toList(n):\n            l = []\n            for i in range(len(nums)):\n                if n % 2:\n                    l.append(i)\n                n = n // 2\n            return l\n        def selectBit(n, k):\n            return [toNum(l) for l in itertools.combinations(toList(n),k)]\n        def getCompatibility(l):\n            if len(set([nums[i] for i in l])) != len(l):\n                return 1000\n            else:\n                x = [nums[i] for i in l]\n                return max(x) - min(x)\n        \n        if len(nums) == k:\n            return 0\n        if k == 1:\n            if len(set(nums)) != len(nums):\n                return -1\n            else:\n                return max(nums) - min(nums)\n        \n        partition_size = len(nums) // k\n        n = len(nums)\n        \n        buckets = [[] for i in range(n+1)]\n        for i in range(1, 1 << n):\n            buckets[countBit(i)].append(i)\n        dp = [1000 for i in range(1 << n)]\n        for j in buckets[partition_size]:\n            dp[j] = getCompatibility(toList(j))\n        for i in range(partition_size * 2, n+1, partition_size):\n            for j in buckets[i]:\n                #print(j, k)\n                for l in selectBit(j, partition_size):\n                    dp[j] = min(dp[j], dp[l] + dp[j-l])\n        if dp[(1 << n) - 1] == 1000:\n            return -1\n        return dp[(1 << n) - 1]\n```
[]