diff --git a/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/1.png b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/1.png new file mode 100644 index 000000000..75c438f49 Binary files /dev/null and b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/1.png differ diff --git a/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/2.png b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/2.png new file mode 100644 index 000000000..52e3549d8 Binary files /dev/null and b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/2.png differ diff --git a/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/README.md b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/README.md index 20e4df9bd..8071c7c1d 100755 --- a/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/README.md +++ b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/README.md @@ -1,28 +1,35 @@ # [1828.Queries on Number of Points Inside a Circle][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## 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:** +**Example 1:** + +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +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:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Queries on Number of Points Inside a Circle -```go ``` - +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. +``` ## 结语 diff --git a/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution.go b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution.go index d115ccf5e..7afe86047 100644 --- a/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution.go +++ b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(points [][]int, queries [][]int) []int { + ans := make([]int, len(queries)) + for idx, q := range queries { + distance := q[2] * q[2] + for _, p := range points { + a := q[0] - p[0] + b := q[1] - p[1] + if a*a+b*b <= distance { + ans[idx]++ + } + } + } + return ans } diff --git a/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution_test.go b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution_test.go index 14ff50eb4..956637562 100644 --- a/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution_test.go +++ b/leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + points, queries [][]int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{1, 3}, {3, 3}, {5, 3}, {2, 2}}, [][]int{{2, 3, 1}, {4, 3, 1}, {1, 1, 2}}, []int{3, 2, 2}}, + {"TestCase2", [][]int{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}, [][]int{{1, 2, 2}, {2, 2, 2}, {4, 3, 2}, {4, 3, 3}}, []int{2, 3, 2, 4}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.points, c.queries) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.points, c.queries) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }