-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
39 lines (34 loc) · 978 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @param {number[]} houses
* @param {number[]} heaters
* @return {number}
*/
var findRadius = function(houses, heaters) {
let res = 0
heaters.sort((a, b) => a - b)
houses.forEach(house => {
let low = 0,
high = heaters.length - 1
while (low <= high) {
let mid = parseInt((low + high) / 2)
if (heaters[mid] < house) {
low = mid + 1
} else if (heaters[mid] > house) {
high = mid - 1
} else {
low = mid
break
}
}
let distance
if (low === 0) {
distance = heaters[low] - house
} else if (low === heaters.length) {
distance = house - heaters[low - 1]
} else {
distance = Math.min(heaters[low] - house, house - heaters[low - 1])
}
res = Math.max(res, distance)
})
return res
};