2517.cpp (546B)
1 class Solution { 2 public: 3 int maximumTastiness(vector<int> &price, int k) { 4 sort(begin(price), end(price)); 5 int low = 0, high = price.back() - price.front(); 6 while (low <= high) { 7 int mid = low + (high - low) / 2, cnt = 1; 8 for (int i = 1, j = 0; i < price.size(); i++) { 9 if (price[i] - price[j] >= mid) cnt++, j = i; 10 } 11 if (cnt >= k) 12 low = mid + 1; 13 else 14 high = mid - 1; 15 } 16 return high; 17 } 18 };