---
# System prepended metadata

title: 1870. Minimum Speed to Arrive on Time
tags: [Leetcode]

---

<style>
    html, body, .ui-content {
    background: #222222;
    color: #00BFFF;
}
::-webkit-scrollbar {
    width: 10px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: linear-gradient(180deg, #2BE8CF60 0%, #2B83E860 100%);
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(180deg, #2BE8CF95 0%, #2B83E895 100%);
}
    /* 設定 code 模板 */
    .markdown-body code,
.markdown-body tt {
    background-color: #ffffff36;
}
.markdown-body .highlight pre,
.markdown-body pre {
    color: #ddd;
    background-color: #00000036;
}
.hljs-tag {
    color: #ddd;
}
.token.operator {
  background-color: transparent;
}
   
</style>

###### tags: `Leetcode`

# 2616. Minimize the Maximum Difference of Pairs

## 題目
###### Link : https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/

## 程式碼

```cpp=
class Solution {
public:
    bool check(vector<int>& nums, int mid, int& p){
        int count = 0, n = nums.size();
        for(int i = 0;i < n - 1 && count < p;){
            if(nums[i + 1] - nums[i] <= mid){
                ++count;
                i += 2;
            }
            else ++i;
        }
        return count >= p;
    }
    int minimizeMax(vector<int>& nums, int p) {
        sort(nums.begin(), nums.end());
        int left = 0, right = nums.back() - nums[0];
        while(left <= right){
            int mid = (left + right) / 2;
            if(check(nums, mid, p)){
                right = mid - 1;
            }
            else{
                left = mid + 1;
            }
        }
        return left;
    }
};
```