Minimum Difference Between Largest and Smallest Value in Three Moves
- Difficulty: Medium
- Type: Sorting
- link
Problem
Solution
- Time Complexity: O(n log n) (for sorting numbers)
- Changing the number is same as excluding it from the candidate for minimum or maximum value (change it to the mean)
import sys
class Solution:
def minDifference(self, nums: List[int]) -> int:
if len(nums) <= 4:
return 0
nums.sort()
front = []
back = []
for i in range(4):
front.append(nums[i])
back.append(nums[-(4-i)])
min_dif = sys.maxsize
for i in range(len(front)):
min_dif = min(min_dif,back[i]-front[i])
return min_dif