1552.cpp (618B)
1 class Solution { 2 public: 3 int maxDistance(vector<int> &position, int m) const { 4 sort(begin(position), end(position)); 5 int low = 0, high = position.back() - position.front(); 6 while (low <= high) { 7 const int mid = low + (high - low) / 2; 8 int prev = position[0], count = 1; 9 for (int i = 1; i < position.size(); i++) { 10 if (position[i] - prev >= mid) prev = position[i], count++; 11 } 12 if (count >= m) 13 low = mid + 1; 14 else 15 high = mid - 1; 16 } 17 return high; 18 } 19 };