Heaters LeetCode solution in JavaScript

1
var findRadius = function (houses, heaters) {
2
houses.sort((a, b) => a - b);
3
heaters.sort((a, b) => a - b);
4
let heaterPos = 0;
5
const getRadius = (house, pos) => Math.abs(heaters[pos] - house);
6

7
return houses.reduce((radius, house) => {
8
while (
9
heaterPos < heaters.length &&
10
getRadius(house, heaterPos) >= getRadius(house, heaterPos + 1)
11
)
12
heaterPos += 1;
13

14
const currentRadius = getRadius(house, heaterPos);
15
return Math.max(radius, currentRadius);
16
}, 0);
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0