Solution Minimum Incompatibility
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public function func($mask, $ValidPermutations, $ValidPValues, &$dp) {\n        if ($mask == 0) {\n            return 0; \n        }\n        if ($dp[$mask] != -1) {\n            return $dp[$mask]; \n        }\n        $ans = PHP_INT_MAX / 2;\n        foreach ($ValidPermutations as $it) {\n            if (($mask & $it) == $it) {\n                $ans1 = $this->func($mask ^ $it, $ValidPermutations, $ValidPValues, $dp);\n                $ans = min($ans, $ans1 + $ValidPValues[$it]);\n            }\n        }\n        return $dp[$mask] = $ans;\n    }\n\n    public function minimumIncompatibility($nums, $k) {\n        $n = count($nums);\n        if ($n % $k != 0) {\n            return -1;\n        }\n        $cnt = $n / $k;\n\n        $ValidPermutations = []; \n        $ValidPValues = []; \n\n        for ($i = 1; $i < (1 << $n); $i++) {\n            $count = 0;\n            $mn = PHP_INT_MAX; \n            $mx = PHP_INT_MIN; \n\n            $st = []; \n            $isRepeat = false;\n            for ($j = 0; $j < $n; $j++) {\n                $mask = 1 << $j; \n                if ($mask & $i) {\n                    if (in_array($nums[$j], $st)) {\n                        $isRepeat = true;\n                        break;\n                    }\n                    $count++;\n                    if ($count > $cnt) {\n                        break;\n                    }\n                    $mn = min($mn, $nums[$j]);\n                    $mx = max($mx, $nums[$j]);\n                    $st[] = $nums[$j];\n                }\n            }\n            if ($count != $cnt) {\n                continue;\n            }\n            if (!$isRepeat) {\n                if ($mx == PHP_INT_MIN || $mn == PHP_INT_MAX) {\n                    continue;\n                }\n                $ValidPermutations[] = $i;\n                $ValidPValues[$i] = $mx - $mn;\n            }\n        }\n\n        $dp = array_fill(0, 1 << $n, -1);\n        $ans = $this->func((1 << $n) - 1, $ValidPermutations, $ValidPValues, $dp);\n        if ($ans >= PHP_INT_MAX / 2) {\n            return -1;\n        }\n        return $ans;\n    }\n}\n\n```
['PHP']