Spread the Sensors
Sensors can be installed at the positions in spots (one per spot). Install exactly k sensors so that the smallest distance between any two sensors is as large as possible, and return that distance.
Requirements
- Binary search on the answer: for a candidate distance , greedily check whether sensors fit.
Constraints
- ;
spots[i]
Examples
EXAMPLE WALKTHROUGHclassic
A guided view of the published example, not a trace of your code.
INPUT
[[1,2,4,8,9],3]TASKspread_the_sensors()
EXPECTED OUTPUT
3Start with the input values shown in this example.
Example 1 of 3
Input: spots = [1,2,4,8,9], k = 3
Output: 3
Explanation: classic
Input: spots = [1,2,3], k = 3
Output: 1
Explanation: tight
Input: spots = [0,1000000000], k = 2
Output: 1000000000
Explanation: two ends
Hint 1
Sort the spots. Feasibility of a distance d is monotonic: if d works, every smaller d works too.
Hint 2
To test d, place the first sensor at the first spot and each next one at the first spot at least d away.