Maximize the Distance Between Points on a Square Problem
Description
LeetCode Problem 3464.
You are given an integer side, representing the edge length of a square with corners at (0, 0), (0, side), (side, 0), and (side, side) on a Cartesian plane.
You are also given a positive integer k and a 2D integer array points, where points[i] = [x_i, y_i] represents the coordinate of a point lying on the boundary of the square.
You need to select k elements among points such that the minimum Manhattan distance between any two points is maximized.
Return the maximum possible minimum Manhattan distance between the selected k points.
The Manhattan Distance between two cells (x_i, y_i) and (x_j, y_j) is |x_i - x_j| + |y_i - y_j|.
Example 1:
Input: side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4
Output: 2
Explanation:

Select all four points.
Example 2:
Input: side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4
Output: 1
Explanation:

Select the points (0, 0), (2, 0), (2, 2), and (2, 1).
Example 3:
Input: side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5
Output: 1
Explanation:

Select the points (0, 0), (0, 1), (0, 2), (1, 2), and (2, 2).
Constraints:
1 <= side <= 10^94 <= points.length <= min(4 * side, 15 * 10^3)points[i] == [x_i, y_i]- The input is generated such that:
points[i]lies on the boundary of the square.- All
points[i]are unique.
4 <= k <= min(25, points.length)
Solution
Approach: Binary Search on the Answer + Greedy on a Circle
To maximize the minimum distance we binary search on the answer d and ask whether k points can be chosen with every pair at least d apart.
Walking around the boundary, a point's Manhattan distance to another point is its distance along the perimeter (or the other way around), so we map each point to a position on a line [0, 4 * side):
x = 0givesy;y = sidegivesside + x;x = sidegives3 * side - y;- otherwise
4 * side - x.
Sort the positions into nums. To check a distance lo, try every point as the start. From the start, greedily jump k - 1 times, each time to the first point at least lo further along (found with binary search). The choice works if every jump succeeds and the last point is at most start + 4 * side - lo, so that the wrap-around gap back to the start is also at least lo.
Complexity: O(n * k * log n * log side) time, O(n) space, where n is the number of points.
from bisect import bisect_left
class Solution:
def maxDistance(self, side: int, points: List[List[int]], k: int) -> int:
nums = []
for x, y in points:
if x == 0:
nums.append(y)
elif y == side:
nums.append(side + x)
elif x == side:
nums.append(side * 3 - y)
else:
nums.append(side * 4 - x)
nums.sort()
def check(lo: int) -> bool:
for start in nums:
end = start + side * 4 - lo
cur = start
ok = True
for _ in range(k - 1):
j = bisect_left(nums, cur + lo)
if j == len(nums) or nums[j] > end:
ok = False
break
cur = nums[j]
if ok:
return True
return False
l, r = 1, side
while l < r:
mid = (l + r + 1) >> 1
if check(mid):
l = mid
else:
r = mid - 1
return l