Ctrl K

Cameras on the Fence

Hard
A plot of land is a square with corners at (0, 0), (0, side), (side, 0) and (side, side). Camera spots are marked on its fence: spots[i] = [x, y] is a spot's position. All spots are distinct and lie on the boundary of the square. Pick exactly cameras spots and mount cameras there so that they are as far apart as possible. The distance between (x₁, y₁) and (x₂, y₂) is |x₁ - x₂| + |y₁ - y₂| (Manhattan distance). Return the largest possible value of the smallest distance between two chosen cameras.
Example 1
side=3, spots=[[0, 0], [0, 3], [3, 3], [3, 0], [1, 0], [3, 2]], cameras=4 → 3

Put the cameras in the four corners: any two of them are at least 3 apart.

Example 2
side=2, spots=[[0, 1], [1, 2], [2, 1], [1, 0], [0, 0]], cameras=5 → 1

All five spots get a camera; the closest pair is (0, 1) and (0, 0), at distance 1.

Example 3
side=4, spots=[[0, 2], [2, 4], [4, 2], [2, 0], [0, 0], [4, 4]], cameras=4 → 4
Constraints
  • 1 ≤ side ≤ 10⁹
  • 4 ≤ len(spots) ≤ min(4 · side, 15,000)
  • Every spots[i] lies on the boundary of the square, and all spots are distinct
  • 4 ≤ cameras ≤ min(25, len(spots))
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Heaviest Reading Day HardTrim Down to a Hill HardFewest Inserts to Reveal a Pattern HardLifters and Energy Drinks HardKeep Every Lamp Running Hard
loading Python…
Input
side=3, spots=[[0, 0], [0, 3], [3, 3], [3, 0], [1, 0], [3, 2]], cameras=4
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 38 tests on the server, hidden ones included.