---
# System prepended metadata

title: Leetcode 2567. Minimum Score by Changing Two Elements
tags: [殘酷 daily, leetcode]

---

# Leetcode 2567. Minimum Score by Changing Two Elements
###### tags: `leetcode` `殘酷 daily`

[題目連結](https://xxxx/)

# Method sort
:::info
:bulb: **作法講解**:
we can change at most two values,
so **the low score always is 0**,

our goal is to find the minimum possible high score,
we know the high score is **maximum value** minus **minimum value**.

so we can sort **"nums"**.


if sorted "nums" like this,
**[A0 A1 A2 A3 A4 A5 A6 A7]**


> if **`A0`** and **`A7`** exists, 
> when we deleted **`A1` ~ `A6`**,
> the high score always is **`A7` - `A0`**,


so we need to remove smallest value or highest value.
we have three case.
1. **remove the two highest values**
   [A0 A1 A2 A3 A4 A5 ~~A6 A7~~]
   
2. **remove the one highest and one lowest values.**
   [~~A0~~ A1 A2 A3 A4 A5 A6 ~~A7~~]
   
3. **remove the two smallest values**
   [~~A0 A1~~ A2 A3 A4 A5 A6 A7]
   
   
we can find the minimum possible high score from above three cases.
:::

TC: O(NlogN) SC: O(1)
完整程式碼
```cpp=
class Solution {
public:
    int minimizeSum(vector<int>& nums) {
        int n = nums.size();
        int output = INT32_MAX;
        sort(nums.begin(), nums.end());

        output = min(output, nums[n-3] - nums[0]);
        output = min(output, nums[n-2] - nums[1]);
        output = min(output, nums[n-1] - nums[2]);
        return output;
    }
};
```



