1485. Signal Tower Reach
A village has houses along a straight road at the coordinates listed in houses (several houses may share a coordinate). You may build exactly k identical signal towers at any positions on the road. A tower at position p with reach r serves every house in [p - r, p + r].
Return the smallest integer reach r >= 0 such that k towers of that reach can serve every house. For a fixed r, a greedy sweep over the sorted houses decides feasibility: put the next tower so that it covers from the leftmost unserved house h up to h + 2r. Feasibility improves as r grows, so binary search r. Expected time is O(n log n) with O(n) extra space.
Example 1
- Input:
- houses = [1,2,4,10], k = 2
- Output:
- 2
- Explanation:
With reach 2 one tower covers houses 1, 2 and 4 and a second covers 10; with reach 1 three towers would be needed.
Example 2
- Input:
- houses = [5,5,5], k = 1
- Output:
- 0
- Explanation:
All houses share one position, so a tower with radius 0 already covers them.
Example 3
- Input:
- houses = [0,100], k = 1
- Output:
- 50
- Explanation:
One tower must span both houses, so it needs to sit at 50 with radius 50.
Constraints
1 ≤ n ≤ 105
0 ≤ houses[i] ≤ 109 (repeated values allowed)
1 ≤ k ≤ n
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(n log n)
- Space
- O(n)