Talentd
FleetCode

2567. Minimum Score by Changing Two Elements

Medium50.4% acceptance

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] and nums[1] to be 6 so that nums 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] and nums[2] to 1 so that nums 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

Hints open one at a time

Hint 1
Changing the minimum or maximum values will only minimize the score.
Hint 2
Think about what all possible pairs of minimum and maximum values can be changed to form the minimum score.
3:48
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minimizeSum(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,4,7,8,5]
Input
[1,4,3]
Your code is saved on this device.Solution