leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2517.cpp (546B)
0 class Solution { 1 public: 2 int maximumTastiness(vector<int> &price, int k) { 3 sort(begin(price), end(price)); 4 int low = 0, high = price.back() - price.front(); 5 while (low <= high) { 6 int mid = low + (high - low) / 2, cnt = 1; 7 for (int i = 1, j = 0; i < price.size(); i++) { 8 if (price[i] - price[j] >= mid) cnt++, j = i; 9 } 10 if (cnt >= k) 11 low = mid + 1; 12 else 13 high = mid - 1; 14 } 15 return high; 16 } 17 };