[Java] Bottom Up Bit Mask DP O(N*3^N)
\nclass Solution {\n    public int minimumIncompatibility(int[] nums, int k) {\n        int n = nums.length;\n        int[] dp = new int[1<<n];\n        int buc
```\nclass Solution {\n    public int minimumIncompatibility(int[] nums, int k) {\n        int n = nums.length;\n        int[] dp = new int[1<<n];\n        int bucketSize = n/k;\n        for(int i=1; i<(1<<n); i++){\n            int count = Integer.bitCount(i);\n            if(count==bucketSize){\n                int max = 0;\n                int min = Integer.MAX_VALUE;\n                int[] freq = new int[n+1];\n                for(int j=0; j<n; j++){\n                    boolean isSet = (i & (1<<j))>0;\n                    if(isSet){\n                        if(++freq[nums[j]]>1){\n                            dp[i] = -1;\n                            break;\n                        }\n                        else{\n                            min = Math.min(min, nums[j]);\n                            max = Math.max(max, nums[j]);\n                        }\n                    }\n                }\n                if(dp[i]!=-1){\n                    dp[i] = max-min;\n                }\n            }\n            else if(count%bucketSize==0){\n                dp[i] = Integer.MAX_VALUE;\n                for(int cur = i; cur>0; cur=(cur-1)&i){\n                    if(Integer.bitCount(cur)==bucketSize){\n                        int comp = i ^ cur;\n                        if(dp[cur]==-1 || dp[comp]==-1){\n                            continue;\n                        }\n                        dp[i] = Math.min(dp[i], dp[cur]+dp[comp]);\n                    }\n                }\n                if(dp[i] == Integer.MAX_VALUE){\n                    dp[i] = -1;\n                }\n            }\n        }\n        return dp[(1<<n)-1];\n    }\n}\n```
[]