Java Top-down DP with memo 28ms beats 88%
\tpublic int minimumIncompatibility(int[] nums, int k) {\n        Arrays.sort(nums);\n        int res = helper(0, nums, k, 0, new Integer[1 << nums.length]);\n 
\tpublic int minimumIncompatibility(int[] nums, int k) {\n        Arrays.sort(nums);\n        int res = helper(0, nums, k, 0, new Integer[1 << nums.length]);\n        return res >= 1000? -1 : res;\n    }\n    \n    private int helper(int blocks, int[] nums, int k, int state, Integer[] memo){\n        if(blocks == k) return 0;\n        if(memo[state] != null) return memo[state];\n        List<int[]> list = new ArrayList();\n        generateNewBlocks(0, 0, 17, -1, nums, state, 0, nums.length / k, list);\n        int res = 1000;\n        for(int[] arr : list){\n            res = Math.min(res, arr[0] + helper(blocks + 1, nums, k, state | arr[1], memo));\n        }\n        return memo[state] = res;\n    }\n    \n    private void generateNewBlocks(int idx, int max, int min, int prev, int[] nums, int state, int mask, int len, List<int[]> list){\n        if(Integer.bitCount(mask) == len){\n            list.add(new int[]{max - min, mask});\n            return;\n        }\n        while(idx < nums.length && ((state & (1 << idx)) != 0 || nums[idx] == prev)) idx ++;\n        if(idx == nums.length) return;\n        if(mask != 0) generateNewBlocks(idx + 1, max, min, prev, nums, state, mask, len, list);\n        int newmask = mask | (1 << idx), newmax = Math.max(max, nums[idx]), newmin = Math.min(min, nums[idx]);\n        generateNewBlocks(idx + 1, newmax, newmin, nums[idx], nums, state, newmask, len, list);\n    }
[]