MediumNo. 2567
Minimum Score by Changing Two Elements — LeetCode 2567 Python Solution
- Pattern
- Greedy
- Topics
- Greedy, Array, Sorting
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given an integer array nums. The low score of nums is the minimum absolute difference between any two integers.
Python solution
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])Complexity
- Time
- O(n log n)
- Space · auxiliary
- O(log n)
Pattern: Greedy
Take the locally best option every time — when you can prove that never costs you later. LeetCode 2567. Minimum Score by Changing Two Elements is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.
The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 2576 | Find the Maximum Number of Marked Indices | Medium |
| 0179 | Largest Number | Medium |
| 0324 | Wiggle Sort II | Medium |
| 0452 | Minimum Number of Arrows to Burst Balloons | Medium |
| 0455 | Assign Cookies | Easy |
| 0502 | IPO | Hard |
Frequently asked questions
- How hard is LeetCode 2567. Minimum Score by Changing Two Elements?
LeetCode 2567. Minimum Score by Changing Two Elements is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2567. Minimum Score by Changing Two Elements?
The Python solution on this page runs in O(n log n).
- What is the space complexity of LeetCode 2567. Minimum Score by Changing Two Elements?
The Python solution on this page uses O(log n) auxiliary space.
- What topics does LeetCode 2567. Minimum Score by Changing Two Elements cover?
LeetCode 2567. Minimum Score by Changing Two Elements is tagged Greedy, Array and Sorting on LeetCode.