Skip to content

Commit dd78e33

Browse files
committed
Add solution and test-cases for problem 1828
1 parent f0a9eb1 commit dd78e33

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed
Loading
Loading

leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [1828.Queries on Number of Points Inside a Circle][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
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`.
7+
8+
For each query `queries[j]`, compute the number of points **inside** the `jth` circle. Points **on the border** of the circle are considered **inside**.
9+
10+
Return an array `answer`, where `answer[j]` is the answer to the `jth` query.
711

8-
**Example 1:**
12+
**Example 1:**
13+
14+
![1](./1.png)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
18+
Output: [3,2,2]
19+
Explanation: The points and circles are shown above.
20+
queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.
1321
```
1422

15-
## 题意
16-
> ...
23+
**EXample 2:**
1724

18-
## 题解
25+
![2](./2.png)
1926

20-
### 思路1
21-
> ...
22-
Queries on Number of Points Inside a Circle
23-
```go
2427
```
25-
28+
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]]
29+
Output: [2,3,2,4]
30+
Explanation: The points and circles are shown above.
31+
queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.
32+
```
2633

2734
## 结语
2835

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(points [][]int, queries [][]int) []int {
4+
ans := make([]int, len(queries))
5+
for idx, q := range queries {
6+
distance := q[2] * q[2]
7+
for _, p := range points {
8+
a := q[0] - p[0]
9+
b := q[1] - p[1]
10+
if a*a+b*b <= distance {
11+
ans[idx]++
12+
}
13+
}
14+
}
15+
return ans
516
}

leetcode/1801-1900/1828.Queries-on-Number-of-Points-Inside-a-Circle/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
points, queries [][]int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 3}, {3, 3}, {5, 3}, {2, 2}}, [][]int{{2, 3, 1}, {4, 3, 1}, {1, 1, 2}}, []int{3, 2, 2}},
17+
{"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}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.points, c.queries)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.points, c.queries)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)