← Questions

Shortest equal length for two boards covering every hole

0

A table top has holes at integer positions along a single line, given as an unsorted array holes (positions may repeat or be negative). You will patch it with exactly two boards that must have the same positive integer length L. A board placed at position X covers every hole whose position is in [X, X + L], both ends included. Boards may be placed anywhere and may overlap.

Implement min_board_length(holes: list[int]) -> int that returns the smallest L for which all holes can be covered.

Constraints

  • 1 <= len(holes) <= 10^5
  • -10^9 <= holes[i] <= 10^9
  • Aim for O(n log n) time.

Example 1

holes = [3, 9, 4]
output: 1

One board covers [3, 4], the other covers [9, 10].

Example 2

holes = [0, 4, 5, 9, 10, 14]
output: 5

Boards at [0, 5] and [9, 14]. Splitting at another gap of 4 would leave one board needing length 10.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding