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