1610.可见点的最大数目
链接:1610.可见点的最大数目
难度:Hard
标签:几何、数组、数学、排序、滑动窗口
简介:返回你能看到的点的最大数目。
题解 1 - typescript
- 编辑时间:2021-12-16
- 执行用时:572ms
- 内存消耗:77.3MB
- 编程语言:typescript
- 解法介绍:遍历每个点获取角度值。
function visiblePoints(points: number[][], angle: number, location: number[]): number {
  const [x, y] = location;
  const list: number[] = [];
  let same = 0;
  for (const [px, py] of points) {
    if (px === x && py === y) {
      same++;
      continue;
    }
    const angle = (Math.atan2(py - y, px - x) * 180) / Math.PI;
    list.push(angle, angle + 360);
  }
  list.sort((a, b) => a - b);
  const n = list.length;
  let l = 0;
  let r = 0;
  let ans = 0;
  while (r < n) {
    while (r < n && list[r] - list[l] <= angle) r++;
    ans = Math.max(ans, r - l);
    l++;
  }
  return ans + same;
}