comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
Medium |
1608 |
Biweekly Contest 98 Q2 |
|
You are given an integer array nums
.
- The low score of
nums
is the minimum absolute difference between any two integers. - The high score of
nums
is the maximum absolute difference between any two integers. - The score of
nums
is the sum of the high and low scores.
Return the minimum score after changing two elements of nums
.
Example 1:
Input: nums = [1,4,7,8,5]
Output: 3
Explanation:
- Change
nums[0]
andnums[1]
to be 6 so thatnums
becomes [6,6,7,8,5]. - The low score is the minimum absolute difference: |6 - 6| = 0.
- The high score is the maximum absolute difference: |8 - 5| = 3.
- The sum of high and low score is 3.
Example 2:
Input: nums = [1,4,3]
Output: 0
Explanation:
- Change
nums[1]
andnums[2]
to 1 so thatnums
becomes [1,1,1]. - The sum of maximum absolute difference and minimum absolute difference is 0.
Constraints:
3 <= nums.length <= 105
1 <= nums[i] <= 109
From the problem description, we know that the minimum score is actually the minimum difference between two adjacent elements in the sorted array, and the maximum score is the difference between the first and last elements of the sorted array. The score of the array
Therefore, we can first sort the array. Since the problem allows us to modify the values of at most two elements in the array, we can modify a number to make it the same as another number in the array, making the minimum score
Modify the smallest two numbers to
The time complexity is
Similar problems:
-1509. Minimum Difference Between Largest and Smallest Value in Three Moves
class Solution:
def minimizeSum(self, nums: List[int]) -> int:
nums.sort()
return min(nums[-1] - nums[2], nums[-2] - nums[1], nums[-3] - nums[0])
class Solution {
public int minimizeSum(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int a = nums[n - 1] - nums[2];
int b = nums[n - 2] - nums[1];
int c = nums[n - 3] - nums[0];
return Math.min(a, Math.min(b, c));
}
}
class Solution {
public:
int minimizeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
return min({nums[n - 1] - nums[2], nums[n - 2] - nums[1], nums[n - 3] - nums[0]});
}
};
func minimizeSum(nums []int) int {
sort.Ints(nums)
n := len(nums)
return min(nums[n-1]-nums[2], min(nums[n-2]-nums[1], nums[n-3]-nums[0]))
}
function minimizeSum(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
return Math.min(nums[n - 3] - nums[0], nums[n - 2] - nums[1], nums[n - 1] - nums[2]);
}
impl Solution {
pub fn minimize_sum(mut nums: Vec<i32>) -> i32 {
nums.sort();
let n = nums.len();
(nums[n - 1] - nums[2])
.min(nums[n - 2] - nums[1])
.min(nums[n - 3] - nums[0])
}
}
#define min(a, b) (((a) < (b)) ? (a) : (b))
int cmp(const void* a, const void* b) {
return *(int*) a - *(int*) b;
}
int minimizeSum(int* nums, int numsSize) {
qsort(nums, numsSize, sizeof(int), cmp);
return min(nums[numsSize - 1] - nums[2], min(nums[numsSize - 2] - nums[1], nums[numsSize - 3] - nums[0]));
}