C++ brute force with 3 steps listed to not TLE
There are 3 parts to this problem to make it not TLE.\n1) Backtracking to save memory.\n2) Don\'t add number to duplicate columns.\n3) Do not search for any sol
There are 3 parts to this problem to make it not TLE.\n1) Backtracking to save memory.\n2) Don\'t add number to duplicate columns.\n3) Do not search for any solution that is less efficient than the found answer.\n\n```\nclass Solution {\npublic:\n    int ans = INT_MAX;\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        if (k == nums.size()) return 0;\n        sort(nums.begin(), nums.end());\n        vector<vector<int>> cur(k, vector<int>(3));\n        dfs(0, nums, cur, nums.size() / k, 0);\n        return ans == INT_MAX ? -1 : ans;\n    }\n    void dfs(int i, vector<int>& n, vector<vector<int>>& c, int s, int sum){        \n            if (i == n.size()) ans = min(ans, sum);\n            if (sum >= ans) return;\n            set<vector<int>> m;\n        for (int y = 0; y < c.size(); ++y){\n            if (c[y][1] == n[i] || c[y][2] == s ||\n                (c[y][2] == 1 && c[y][0] == n[i]) || m.find(c[y]) != m.end()) continue;\n            m.insert(c[y]);\n            if (c[y][2] == 0){\n                c[y][1] = c[y][0] = n[i], ++c[y][2];\n                dfs(i + 1, n, c, s, sum);\n                c[y][1] = c[y][2] = c[y][0] = 0;\n            }\n            else{\n                int prev = c[y][1];\n                c[y][1] = n[i], ++c[y][2];\n                dfs(i + 1, n, c, s, sum - (prev - c[y][0]) + (c[y][1] - c[y][0]));\n                c[y][1] = prev, --c[y][2];\n            }\n        }\n    }\n};\n```
[]