All files / leetCode 0475.ts

100% Statements 11/11
100% Branches 2/2
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24        3x 3x   6x 3x   3x 9x       1x     9x     3x    
export default function findRadius(
  houses: number[],
  heaters: number[],
): number {
  let radius = 0
  let index = 0
 
  houses.sort((a, b) => a - b)
  heaters.sort((a, b) => a - b)
 
  for (const house of houses) {
    while (
      index + 1 < heaters.length
      && Math.abs(heaters[index + 1] - house) <= Math.abs(heaters[index] - house)
    ) {
      index++
    }
 
    radius = Math.max(radius, Math.abs(heaters[index] - house))
  }
 
  return radius
}