Skip to content

Latest commit

 

History

History
162 lines (135 loc) · 5.27 KB

File metadata and controls

162 lines (135 loc) · 5.27 KB

中文文档

Description

You are given an array points where points[i] = [xi, yi] is the coordinates of the ith point on a 2D plane. Multiple points can have the same coordinates.

You are also given an array queries where queries[j] = [xj, yj, rj] describes a circle centered at (xj, yj) with a radius of rj.

For each query queries[j], compute the number of points inside the jth circle. Points on the border of the circle are considered inside.

Return an array answer, where answer[j] is the answer to the jth query.

 

Example 1:

Input: points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
Output: [3,2,2]
Explanation: The points and circles are shown above.
queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.

Example 2:

Input: points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
Output: [2,3,2,4]
Explanation: The points and circles are shown above.
queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.

 

Constraints:

  • 1 <= points.length <= 500
  • points[i].length == 2
  • 0 <= x​​​​​​i, y​​​​​​i <= 500
  • 1 <= queries.length <= 500
  • queries[j].length == 3
  • 0 <= xj, yj <= 500
  • 1 <= rj <= 500
  • All coordinates are integers.

 

Follow up: Could you find the answer for each query in better complexity than O(n)?

Solutions

Python3

class Solution:
    def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
        ans = []
        for x0, y0, r in queries:
            count = 0
            for x, y in points:
                dx, dy = x - x0, y - y0
                if dx * dx + dy * dy <= r * r:
                    count += 1
            ans.append(count)
        return ans

Java

class Solution {
    public int[] countPoints(int[][] points, int[][] queries) {
        int[] ans = new int[queries.length];
        int i = 0;
        for (int[] query : queries) {
            int x0 = query[0], y0 = query[1], r = query[2];
            for (int[] point : points) {
                int x = point[0], y = point[1];
                int dx = x - x0, dy = y - y0;
                if (dx * dx + dy * dy <= r * r) {
                    ++ans[i];
                }
            }
            ++i;
        }
        return ans;
    }
}

TypeScript

function countPoints(points: number[][], queries: number[][]): number[] {
    let ans = [];
    for (let [cx, cy, r] of queries) {
        let square = r ** 2;
        let count = 0;
        for (let [px, py] of points) {
            if ((px - cx) ** 2 + (py - cy) ** 2 <= square) {
                ++count;
            }
        }
        ans.push(count);
    }
    return ans;
};

C++

class Solution {
public:
    vector<int> countPoints(vector<vector<int>>& points, vector<vector<int>>& queries) {
        vector<int> ans;
        for (auto& query : queries) {
            int x0 = query[0], y0 = query[1], r = query[2];
            int count = 0;
            for (auto& point : points) {
                int x = point[0], y = point[1];
                int dx = x - x0, dy = y - y0;
                if (dx * dx + dy * dy <= r * r) {
                    ++count;
                }
            }
            ans.push_back(count);
        }
        return ans;
    }
};

Go

func countPoints(points [][]int, queries [][]int) []int {
	ans := make([]int, len(queries))
	for i, query := range queries {
		x0, y0, r := query[0], query[1], query[2]
		for _, point := range points {
			x, y := point[0], point[1]
			dx, dy := x-x0, y-y0
			if dx*dx+dy*dy <= r*r {
				ans[i]++
			}
		}
	}
	return ans
}

...