Maximize the Distance Between Points on a Square
Real-world version: Spreading Marks Round a Square FenceThe statement here is our own: the same inputs, outputs and technique.
A square fence has its corners at (0, 0), (side, 0), (side, side) and (0, side). Candidate posts are given as points, each [x, y] lying on the fence itself, and all of them are distinct.
Choose k of the posts. The spread of a choice is the smallest distance between any two posts chosen, measured as the difference in x plus the difference in y.
Return the largest spread any choice of k posts can have.
Examples
Example 1
- Input
- side = 12, points = [[0, 0], [12, 0], [12, 12], [0, 12], [6, 0], [12, 6]], k = 4
- Output
- 12
Taking the four corners leaves every neighbouring pair 12 apart, and no choice of four posts spreads them further.
Example 2
- Input
- side = 2, points = [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 2], [0, 2], [0, 1]], k = 8
- Output
- 1
Every post on the fence has to be taken, and the closest neighbouring pair sits one apart.
Example 3
- Input
- side = 1000000000, points = [[0, 0], [1000000000, 0], [1000000000, 1000000000], [0, 1000000000]], k = 4
- Output
- 1000000000
Only the four corners are on offer, so the spread is the side length itself.
Constraints
- 1 <= side <= 10^9
- 4 <= points.length <= 1000
- points[i].length == 2
- Every point lies on the fence
- All the points are different
- 4 <= k <= points.length
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def max_distance(side: int, points: list[list[int]], k: int) -> int:public int maxDistance(int side, int[][] points, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Missing NumberReal-world version: Unclaimed Badge NumberEasy
- K Closest Points to OriginReal-world version: Nearest Survey StakesMedium
- Sell Diminishing-Valued Colored BallsReal-world version: Rare Poster RevenueMedium
- Minimum Operations to Transform Array into Alternating PrimeReal-world version: Nudging Readings onto Prime ValuesMedium