[Java] Double Bitmasking DP +  Pruning + Early Halt
\nclass Solution {\n    public int minimumIncompatibility(int[] nums, int k) {\n        int n = nums.length;\n        int size = n / k;\n        int complete = 
```\nclass Solution {\n    public int minimumIncompatibility(int[] nums, int k) {\n        int n = nums.length;\n        int size = n / k;\n        int complete = (1 << n) - 1;\n        if (k == nums.length) {\n            return 0;\n        }\n        int[] cnt = new int[17];\n        for (int x : nums) {\n            cnt[x]++;\n        }\n        for (int i = 0; i < 17; i++) {\n            if (cnt[i] > k) {\n                return -1;\n            }\n        }\n        Map<String, Integer> dp = new HashMap<>();\n        Arrays.sort(nums);\n        return dfs(0, -1, nums, Integer.MAX_VALUE, Integer.MIN_VALUE, 0, 0, size, complete, dp, true);\n    }\n    \n    private int dfs(int mask, int index, int[] nums, int min, int max, int set, int cnt, int limit, int complete, Map<String, Integer> dp, boolean halt) {\n        String key = mask + " " + set;\n        if (mask == complete) {\n            return max - min;\n        }\n        if (dp.containsKey(key)) {\n            return dp.get(key);\n        }\n        int res = Integer.MAX_VALUE;\n        int add = 0;\n        if (cnt == limit) {\n            set = 0;\n            cnt = 0;\n            add = max - min;\n            min = Integer.MAX_VALUE;\n            max = Integer.MIN_VALUE;\n            index = -1;\n            halt = true;\n        }\n        int last = -1;\n        for (int i = index + 1; i < nums.length; i++) {\n            if ((mask & (1 << i)) != 0) {\n                continue;\n            }\n            if ((set & (1 << nums[i])) != 0) {\n                continue;\n            }\n            if (nums[i] == last) {\n                continue;\n            }\n            int next = dfs(mask | (1 << i), i, nums, Math.min(min, nums[i]), Math.max(max, nums[i]), set | (1 << nums[i]), cnt + 1, limit, complete, dp, false);\n            res = Math.min(res, next);\n            last = nums[i];\n            if (halt) {\n                break;\n            }\n        }\n        if (res != Integer.MAX_VALUE) {\n            res += add;\n        }\n        dp.put(key, res);\n        return res;\n    }\n}\n```
[]