Python short and clean. Beats 100%. 2 solutions. Sorting and Heap.
Approach 1: Sort\n1. Sort nums to s_nums for random access to maxes and mins.\n\n2. If there were 0 changes allowed:\n    There\'s nothing to do.\n    The best 
# Approach 1: Sort\n1. Sort `nums` to `s_nums` for random access to maxes and mins.\n\n2. If there were `0` changes allowed:\n    There\'s nothing to do.\n    The best we can do is: `s_nums[n - 1] - s_nums[0]`.\n    \n    Note: Abbriviating `s_num[a] - s_num[b]` as `|a, b|` from henceforth.\n    Hence the above becomes `|n - 1, 0|`\n\n3. If there were `1` change allowed:\n    We can change the first or the last number to the adjacent one.\n    The best we can do is:\n    `min(|n - 1, 1| (changing the first), |n - 2, 0| (changing the last)`.\n\n4. If there were `2` change allowed:\n    The best we can do is:\n    `min(|n - 1, 2|, |n - 2, 1|, |n - 3, 0|)`\n\n5. Extending the same, If there were `k` changes allowed:\n    The best we can do is:\n    `min(|n - 1, k|, |n - 2, k - 1|, ...., |n - k - 1, 0|)`\n\n# Complexity\n- Time complexity: $$O(n * log(n))$$\n\n- Space complexity: $$O(n)$$\n\nwhere, `n is the length of nums`.\n\n# Code\n```python\nclass Solution:\n    def minimizeSum(self, nums: list[int]) -> int:\n        s_nums = sorted(nums)\n        k = 2\n        return min(\n            s_nums[i] - s_nums[j]\n            for i, j in zip(range(-1, -k - 2, -1), range(k, -1, -1))\n        )\n\n\n```\n\n---\n\n# Approach 2: Heap\nSince we only need first `k` minimum and first `k` maximum elements in `nums`, given atmost `k` changes allowed, we can use a `min heap` and `max heap` respectively.\n\nThis allows to reduce the time complexity from $$n + n * log(n)$$ to $$n + k * log(n)$$.\n\n# Complexity\n- Time complexity: $$O(n + k * log(n))$$\nFor given question, `k = 2`, `=>` $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\nwhere,\n`n is the length of nums`,\n`k is the number of number of values that can be changed`.\n\n# Code\n```python\nclass Solution:\n    def minimizeSum(self, nums: list[int]) -> int:\n        k, n = 2, len(nums)\n        min_hq, max_hq = list(nums), list(map(neg, nums))\n        heapify(min_hq); heapify(max_hq)\n\n        maxs = tuple(-heappop(max_hq) for _ in range(min(k + 1, n)))\n        mins = tuple( heappop(min_hq) for _ in range(min(k + 1, n)))\n\n        return min(map(sub, maxs, reversed(mins)))\n\n\n```
['Math', 'Sorting', 'Heap (Priority Queue)', 'Python', 'Python3']