Good afternoon! Here's our prompt for today.
On a given street, there's a bunch of houses and lamps nearby. It looks kind of like the following (H
being houses, L
being lamps):
1L H L H H
20 1 2 3 4
We decide to represent this with two arrays: one being the positions of the houses, and the other the positions of the lamps. From the above example, the arrays would be:
1houses = [1, 3, 4]
2lamps = [0, 2]
With this knowledge, can you find out the minimum radius
that the lamps would need to cover in order for every house to get access to light?

Here, the minimum radius would be 2
-- the lamp at position 2
needs to cover not only house 3
, but up to the house on position 4
, so a 2
radius is required.
1L H L H H
20 1 2 3 4
We can guarantee that there is at least one house and one lamp in every scenario. All lamps cover the same minimum radius.
Constraints
- Length of the given array <=
100000
- Values in the array ranges from
0
to100000
- Expected time complexity :
O(n^2)
- Expected space complexity :
O(1)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
var assert = require('assert');
​
function getRadius(houses, lamps) {
// fill this in
return radius;
}
​
console.log(getRadius([1, 2, 3], [2]));
​
try {
assert.equal(getRadius([1, 2, 3], [2]), 1);
​
console.log('PASSED: ' + 'Expect `getRadius([1,2,3],[2])` to return `1`');
} catch (err) {
console.log(err);
}
​
try {
assert.equal(getRadius([2, 3, 8], [2]), 6);
​
console.log('PASSED: ' + 'Expect `getRadius([2,3, 8], [2])` to return `6`');
} catch (err) {
console.log(err);
}
​
We'll now take you through what you need to know.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.