返回介绍

solution / 2500-2599 / 2567.Minimum Score by Changing Two Elements / README_EN

发布于 2024-06-17 01:03:04 字数 6038 浏览 0 评论 0 收藏 0

2567. Minimum Score by Changing Two Elements

中文文档

Description

You are given a 0-indexed integer array nums.

  • The low score of nums is the minimum value of |nums[i] - nums[j]| over all 0 <= i < j < nums.length.
  • The high score of nums is the maximum value of |nums[i] - nums[j]| over all 0 <= i < j < nums.length.
  • The score of nums is the sum of the high and low scores of nums.

To minimize the score of nums, we can change the value of at most two elements of nums.

Return _the minimum possible score after changing the value of at most two elements o_f nums.

Note that |x| denotes the absolute value of x.

 

Example 1:

Input: nums = [1,4,3]
Output: 0
Explanation: Change value of nums[1] and nums[2] to 1 so that nums becomes [1,1,1]. Now, the value of |nums[i] - nums[j]| is always equal to 0, so we return 0 + 0 = 0.

Example 2:

Input: nums = [1,4,7,8,5]
Output: 3
Explanation: Change nums[0] and nums[1] to be 6. Now nums becomes [6,6,7,8,5].
Our low score is achieved when i = 0 and j = 1, in which case |nums[i] - nums[j]| = |6 - 6| = 0.
Our high score is achieved when i = 3 and j = 4, in which case |nums[i] - nums[j]| = |8 - 5| = 3.
The sum of our high and low score is 3, which we can prove to be minimal.

 

Constraints:

  • 3 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Solutions

Solution 1: Sorting + Greedy

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 $nums$ is the sum of the minimum score and the maximum score.

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 $0$. In this case, the score of the array $nums$ is actually the maximum score. We can choose to make one of the following modifications:

Modify the smallest two numbers to $nums[2]$, then the maximum score is $nums[n - 1] - nums[2]$; Modify the smallest number to $nums[1]$ and the largest number to $nums[n - 2]$, then the maximum score is $nums[n - 2] - nums[1]$; Modify the largest two numbers to $nums[n - 3]$, then the maximum score is $nums[n - 3] - nums[0]$. Finally, we return the minimum score of the above three modifications.

The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $nums$.

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]));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文