返回介绍

solution / 2200-2299 / 2249.Count Lattice Points Inside a Circle / README_EN

发布于 2024-06-17 01:03:08 字数 5287 浏览 0 评论 0 收藏 0

2249. Count Lattice Points Inside a Circle

中文文档

Description

Given a 2D integer array circles where circles[i] = [xi, yi, ri] represents the center (xi, yi) and radius ri of the ith circle drawn on a grid, return _the number of lattice points __that are present inside at least one circle_.

Note:

  • A lattice point is a point with integer coordinates.
  • Points that lie on the circumference of a circle are also considered to be inside it.

 

Example 1:

Input: circles = [[2,2,1]]
Output: 5
Explanation:
The figure above shows the given circle.
The lattice points present inside the circle are (1, 2), (2, 1), (2, 2), (2, 3), and (3, 2) and are shown in green.
Other points such as (1, 1) and (1, 3), which are shown in red, are not considered inside the circle.
Hence, the number of lattice points present inside at least one circle is 5.

Example 2:

Input: circles = [[2,2,2],[3,4,1]]
Output: 16
Explanation:
The figure above shows the given circles.
There are exactly 16 lattice points which are present inside at least one circle. 
Some of them are (0, 2), (2, 0), (2, 4), (3, 2), and (4, 4).

 

Constraints:

  • 1 <= circles.length <= 200
  • circles[i].length == 3
  • 1 <= xi, yi <= 100
  • 1 <= ri <= min(xi, yi)

Solutions

Solution 1

class Solution:
  def countLatticePoints(self, circles: List[List[int]]) -> int:
    ans = 0
    mx = max(x + r for x, _, r in circles)
    my = max(y + r for _, y, r in circles)
    for i in range(mx + 1):
      for j in range(my + 1):
        for x, y, r in circles:
          dx, dy = i - x, j - y
          if dx * dx + dy * dy <= r * r:
            ans += 1
            break
    return ans
class Solution {
  public int countLatticePoints(int[][] circles) {
    int mx = 0, my = 0;
    for (var c : circles) {
      mx = Math.max(mx, c[0] + c[2]);
      my = Math.max(my, c[1] + c[2]);
    }
    int ans = 0;
    for (int i = 0; i <= mx; ++i) {
      for (int j = 0; j <= my; ++j) {
        for (var c : circles) {
          int dx = i - c[0], dy = j - c[1];
          if (dx * dx + dy * dy <= c[2] * c[2]) {
            ++ans;
            break;
          }
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countLatticePoints(vector<vector<int>>& circles) {
    int mx = 0, my = 0;
    for (auto& c : circles) {
      mx = max(mx, c[0] + c[2]);
      my = max(my, c[1] + c[2]);
    }
    int ans = 0;
    for (int i = 0; i <= mx; ++i) {
      for (int j = 0; j <= my; ++j) {
        for (auto& c : circles) {
          int dx = i - c[0], dy = j - c[1];
          if (dx * dx + dy * dy <= c[2] * c[2]) {
            ++ans;
            break;
          }
        }
      }
    }
    return ans;
  }
};
func countLatticePoints(circles [][]int) (ans int) {
  mx, my := 0, 0
  for _, c := range circles {
    mx = max(mx, c[0]+c[2])
    my = max(my, c[1]+c[2])
  }
  for i := 0; i <= mx; i++ {
    for j := 0; j <= my; j++ {
      for _, c := range circles {
        dx, dy := i-c[0], j-c[1]
        if dx*dx+dy*dy <= c[2]*c[2] {
          ans++
          break
        }
      }
    }
  }
  return
}
function countLatticePoints(circles: number[][]): number {
  let mx = 0;
  let my = 0;
  for (const [x, y, r] of circles) {
    mx = Math.max(mx, x + r);
    my = Math.max(my, y + r);
  }
  let ans = 0;
  for (let i = 0; i <= mx; ++i) {
    for (let j = 0; j <= my; ++j) {
      for (const [x, y, r] of circles) {
        const dx = i - x;
        const dy = j - y;
        if (dx * dx + dy * dy <= r * r) {
          ++ans;
          break;
        }
      }
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文