Seems bug  got TLE with all passed?
O(3 ^ n) algorithm\n\n\nclass Solution {\n    void better(int& x, int y, int z) {\n        if (y < 0 || z < 0) return;\n        y += z;\n        if (x < 0 || x 
O(3 ^ n) algorithm\n\n```\nclass Solution {\n    void better(int& x, int y, int z) {\n        if (y < 0 || z < 0) return;\n        y += z;\n        if (x < 0 || x > y) {\n            x = y;\n        }\n    }\n\n    void dfs(const vector<int>& nums, const int want, const int ind, const int mask, const int have, const int mini, const int maxi, vector<int>& all) {\n        if (want < 0 || nums.size() - ind < want) return;\n        if (ind == nums.size()) {\n            all[mask] = maxi - mini;\n            return;\n        }\n        dfs(nums, want, ind + 1, mask, have, mini, maxi, all);\n        if ((have & (1 << nums[ind])) == 0) {\n            dfs(nums, want - 1, ind + 1, mask | (1 << ind), have | (1 << nums[ind]), \n                min(mini, nums[ind]), max(maxi, nums[ind]), all);\n            \n        }\n    }\npublic:\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        const int n = nums.size(), m = 1 << n;\n        if (n == k) return 0;\n        vector<int> all(m, -1);\n        dfs(nums, n / k, 0, 0, 0, 12345, -12345, all);\n        vector<int> dp(m, -1);\n        dp[0] = 0;\n        for (int i = 1; i < m; ++i) {\n            for (int j = (i - 1) & i;; j = (j - 1) & i) {\n                better(dp[i], dp[j], all[i ^ j]);\n                if (j == 0) break;\n            }\n        }\n        return dp[m - 1];\n    }\n};\n```\n\nThis says all passed.\n![image](https://assets.leetcode.com/users/images/eb3a9067-f0c2-4124-9025-122fd0dae602_1607230943.521663.png)\n\n
[]