# 2616. Minimize the Maximum Difference of Pairs ###### tags: `Leetcode` `Medium` `Greedy` `Binary Search` Link: https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/description/ ## 思路 binary search by value找答案 假设mid就是maximum difference 数有多少个pairs满足条件 数的时候用到了greedy 从前往后遍历 只要碰到两个相邻的数字差小于mid 就把这个pair算进来 cnt加一 ## Code ```python= class Solution: def minimizeMax(self, nums: List[int], p: int) -> int: nums.sort() n = len(nums) start, end = 0, nums[-1]-nums[0] while start<end: mid = start+(end-start)//2 cnt, i = 0, 1 while i<n: if nums[i]-nums[i-1]<=mid: cnt += 1 i += 1 i += 1 if cnt<p: start = mid+1 else: end = mid return start ```