All files / leetCode 0447.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 1/1
100% Lines 11/11

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  4x   4x   12x   12x 44x 44x 44x 44x       12x   35x       4x    
export default function numberOfBoomerangs(points: number[][]): number {
  let res = 0
 
  for (let i = 0; i < points.length; i++) {
    // `i` is the center point
    const map = new Map<number, number>()
 
    for (let j = 0; j < points.length; j++) {
      const dx = points[i][0] - points[j][0]
      const dy = points[i][1] - points[j][1]
      const distance = dx * dx + dy * dy
      map.set(distance, (map.get(distance) ?? 0) + 1)
    }
 
    // Find left 2 points
    for (const count of map.values()) {
      // 组合: C(count, 2)
      res += count * (count - 1)
    }
  }
 
  return res
}