KatInterview

Minimum Guards to Cover All Museum Gallery Segments

Greedy & IntervalsAuto-graded · 17 test cases
HardAuto-gradedGreedy & Intervalsgreedyintervalssweep line

Last updated

Tests ability to apply a sweep-line with a greedy exchange argument to optimally place point guards that each cover a fixed-radius interval on a 1-D gallery.

Minimum Guards to Cover All Museum Gallery Segments

A museum has a long corridor modelled as a number line. Several exhibits are placed along it; each exhibit occupies a closed segment [l, r] (with l ≤ r) that must be fully covered by at least one guard.

You may place guards anywhere on the number line (including non-integer positions). Each guard has a coverage radius d: a guard placed at position p covers every point in [p - d, p + d].

Find the minimum number of guards needed so that every exhibit segment is fully covered (i.e., every point of every segment [l, r] lies within the coverage interval of at least one guard).

Task

Implement:

def min_guards(segments: list[list[int]], d: int) -> int:
  • segments: a list of segments, each given as [l, r] with l ≤ r.
  • d: the non-negative integer coverage radius of each guard.
  • Returns the minimum number of guards required to cover all segments, or -1 if it is impossible (i.e., some segment is longer than 2d and therefore cannot be covered by any single guard — but note a segment may be covered by multiple guards; see constraints for when -1 applies).

How coverage works

A segment [l, r] is fully covered if and only if every point x ∈ [l, r] belongs to [p - d, p + d] for at least one guard position p. Because a single guard covers a contiguous interval of length 2d, a segment longer than 2d cannot be covered by a single guard. However, multiple guards can cooperate to cover a long segment — there is no restriction on the number of guards or their placement, and their coverage intervals may overlap.

Wait — re-read: multiple guards can cooperate. So -1 is actually never returned (you can always add more guards). Therefore the function always returns a non-negative integer.

Clarification on -1: The return value is always ≥ 0. If segments is empty, return 0.

Key insight

Because guards can cooperate, no segment is impossible to cover. The challenge is finding the minimum count via an optimal greedy sweep.

Approach hint

Think about processing segments sorted by their right endpoint and greedily placing each guard as far right as possible while still covering the current uncovered portion.

Constraints

  • 0 ≤ len(segments) ≤ 2000
  • -10^6 ≤ l ≤ r ≤ 10^6 for each segment
  • 0 ≤ d ≤ 10^6
  • Segments may overlap, be nested, or be identical.
  • l and r are integers, but optimal guard positions may be non-integers (your algorithm only needs to return the count).

Examples

Example 1

Input: min_guards([[1, 5], [8, 10]], 3)

Output: 2

Example 2

Input: min_guards([[0, 0], [5, 5], [10, 10]], 0)

Output: 3

Example 3

Input: min_guards([[2, 14]], 3)

Output: 2

Hints

Hint 1

Sort segments by right endpoint (and break ties by left endpoint). Process them left-to-right: the 'hardest' constraint at each step is the segment whose right end comes soonest.

Hint 2

For the current uncovered leftmost point, a guard must be placed no later than (leftmost_uncovered + d) to reach it, and that guard then covers up to (leftmost_uncovered + 2d). Use this to advance your 'covered up to' pointer greedily.

Hint 3

After placing a guard optimally, some later segments may already be fully covered — skip them. Only restart when you encounter a segment whose left endpoint exceeds your current coverage boundary.