/**
* Problem: LeetCode 2567 - Minimum Score by Changing Two Elements
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.Arrays;
class Solution {
public int minimizeSum(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
// Low score can always be made 0 by duplicating an element.
// Therefore, we only need to minimize the high score (max - min) after changing 2 elements.
int opt1 = nums[n - 1] - nums[2]; // Change 2 smallest elements
int opt2 = nums[n - 2] - nums[1]; // Change 1 smallest + 1 largest element
int opt3 = nums[n - 3] - nums[0]; // Change 2 largest elements
return Math.min(opt1, Math.min(opt2, opt3));
}
}
"""
Problem: LeetCode 2567 - Minimum Score by Changing Two Elements
Difficulty: Medium
Language: Python 3
Author: CodeByTushu
"""
from typing import List
class Solution:
def minimizeSum(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
# Low score can always be 0 by making two elements identical.
# Thus, we only need to minimize the high score (max - min) after modifying 2 elements.
opt1 = nums[n - 1] - nums[2] # Modify 2 smallest elements
opt2 = nums[n - 2] - nums[1] # Modify 1 smallest + 1 largest element
opt3 = nums[n - 3] - nums[0] # Modify 2 largest elements
return min(opt1, opt2, opt3)
/**
* Problem: LeetCode 2567 - Minimum Score by Changing Two Elements
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int minimizeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
// Low score can always be made 0 by duplicating an element.
// Therefore, we only need to minimize the high score (max - min).
int opt1 = nums[n - 1] - nums[2]; // Modify 2 smallest elements
int opt2 = nums[n - 2] - nums[1]; // Modify 1 smallest + 1 largest element
int opt3 = nums[n - 3] - nums[0]; // Modify 2 largest elements
return min({opt1, opt2, opt3});
}
};
/**
* Problem: LeetCode 2567 - Minimum Score by Changing Two Elements
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[]} nums
* @return {number}
*/
var minimizeSum = function(nums) {
nums.sort((a, b) => a - b);
const n = nums.length;
// Low score can always be 0 by making two elements equal.
// We only need to minimize the high score (max - min) after modifying 2 elements.
const opt1 = nums[n - 1] - nums[2]; // Modify 2 smallest elements
const opt2 = nums[n - 2] - nums[1]; // Modify 1 smallest + 1 largest element
const opt3 = nums[n - 3] - nums[0]; // Modify 2 largest elements
return Math.min(opt1, opt2, opt3);
};