Skip to content

Commit b2046fe

Browse files
committed
feat: add solutions to lc problem: No.1828
No.1828.Queries on Number of Points Inside a Circle
1 parent 5810fbf commit b2046fe

File tree

5 files changed

+179
-48
lines changed

5 files changed

+179
-48
lines changed

solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README.md

+66-19
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,6 @@ class Solution {
100100
}
101101
```
102102

103-
### **TypeScript**
104-
105-
```ts
106-
function countPoints(points: number[][], queries: number[][]): number[] {
107-
let ans = [];
108-
for (let [cx, cy, r] of queries) {
109-
let square = r ** 2;
110-
let count = 0;
111-
for (let [px, py] of points) {
112-
if ((px - cx) ** 2 + (py - cy) ** 2 <= square) {
113-
++count;
114-
}
115-
}
116-
ans.push(count);
117-
}
118-
return ans;
119-
}
120-
```
121-
122103
### **C++**
123104

124105
```cpp
@@ -162,6 +143,72 @@ func countPoints(points [][]int, queries [][]int) []int {
162143
}
163144
```
164145

146+
### **TypeScript**
147+
148+
```ts
149+
function countPoints(points: number[][], queries: number[][]): number[] {
150+
return queries.map(([cx, cy, r]) => {
151+
let res = 0;
152+
for (const [px, py] of points) {
153+
if (Math.sqrt((cx - px) ** 2 + (cy - py) ** 2) <= r) {
154+
res++;
155+
}
156+
}
157+
return res;
158+
});
159+
}
160+
```
161+
162+
### **Rust**
163+
164+
```rust
165+
impl Solution {
166+
pub fn count_points(points: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {
167+
queries
168+
.iter()
169+
.map(|v| {
170+
let cx = v[0];
171+
let cy = v[1];
172+
let r = v[2].pow(2);
173+
let mut count = 0;
174+
for p in points.iter() {
175+
if ((p[0] - cx).pow(2) + (p[1] - cy).pow(2)) <= r {
176+
count += 1;
177+
}
178+
}
179+
count
180+
})
181+
.collect()
182+
}
183+
}
184+
```
185+
186+
### **C**
187+
188+
```c
189+
/**
190+
* Note: The returned array must be malloced, assume caller calls free().
191+
*/
192+
int *countPoints(int **points, int pointsSize, int *pointsColSize, int **queries, int queriesSize, int *queriesColSize,
193+
int *returnSize) {
194+
int *ans = malloc(sizeof(int) * queriesSize);
195+
for (int i = 0; i < queriesSize; i++) {
196+
int cx = queries[i][0];
197+
int cy = queries[i][1];
198+
int r = queries[i][2];
199+
int count = 0;
200+
for (int j = 0; j < pointsSize; j++) {
201+
if (sqrt(pow(points[j][0] - cx, 2) + pow(points[j][1] - cy, 2)) <= r) {
202+
count++;
203+
}
204+
}
205+
ans[i] = count;
206+
}
207+
*returnSize = queriesSize;
208+
return ans;
209+
}
210+
```
211+
165212
### **...**
166213
167214
```

solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README_EN.md

+66-19
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,6 @@ class Solution {
9393
}
9494
```
9595

96-
### **TypeScript**
97-
98-
```ts
99-
function countPoints(points: number[][], queries: number[][]): number[] {
100-
let ans = [];
101-
for (let [cx, cy, r] of queries) {
102-
let square = r ** 2;
103-
let count = 0;
104-
for (let [px, py] of points) {
105-
if ((px - cx) ** 2 + (py - cy) ** 2 <= square) {
106-
++count;
107-
}
108-
}
109-
ans.push(count);
110-
}
111-
return ans;
112-
}
113-
```
114-
11596
### **C++**
11697

11798
```cpp
@@ -155,6 +136,72 @@ func countPoints(points [][]int, queries [][]int) []int {
155136
}
156137
```
157138

139+
### **TypeScript**
140+
141+
```ts
142+
function countPoints(points: number[][], queries: number[][]): number[] {
143+
return queries.map(([cx, cy, r]) => {
144+
let res = 0;
145+
for (const [px, py] of points) {
146+
if (Math.sqrt((cx - px) ** 2 + (cy - py) ** 2) <= r) {
147+
res++;
148+
}
149+
}
150+
return res;
151+
});
152+
}
153+
```
154+
155+
### **Rust**
156+
157+
```rust
158+
impl Solution {
159+
pub fn count_points(points: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {
160+
queries
161+
.iter()
162+
.map(|v| {
163+
let cx = v[0];
164+
let cy = v[1];
165+
let r = v[2].pow(2);
166+
let mut count = 0;
167+
for p in points.iter() {
168+
if ((p[0] - cx).pow(2) + (p[1] - cy).pow(2)) <= r {
169+
count += 1;
170+
}
171+
}
172+
count
173+
})
174+
.collect()
175+
}
176+
}
177+
```
178+
179+
### **C**
180+
181+
```c
182+
/**
183+
* Note: The returned array must be malloced, assume caller calls free().
184+
*/
185+
int *countPoints(int **points, int pointsSize, int *pointsColSize, int **queries, int queriesSize, int *queriesColSize,
186+
int *returnSize) {
187+
int *ans = malloc(sizeof(int) * queriesSize);
188+
for (int i = 0; i < queriesSize; i++) {
189+
int cx = queries[i][0];
190+
int cy = queries[i][1];
191+
int r = queries[i][2];
192+
int count = 0;
193+
for (int j = 0; j < pointsSize; j++) {
194+
if (sqrt(pow(points[j][0] - cx, 2) + pow(points[j][1] - cy, 2)) <= r) {
195+
count++;
196+
}
197+
}
198+
ans[i] = count;
199+
}
200+
*returnSize = queriesSize;
201+
return ans;
202+
}
203+
```
204+
158205
### **...**
159206
160207
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int *countPoints(int **points, int pointsSize, int *pointsColSize, int **queries, int queriesSize, int *queriesColSize,
5+
int *returnSize) {
6+
int *ans = malloc(sizeof(int) * queriesSize);
7+
for (int i = 0; i < queriesSize; i++) {
8+
int cx = queries[i][0];
9+
int cy = queries[i][1];
10+
int r = queries[i][2];
11+
int count = 0;
12+
for (int j = 0; j < pointsSize; j++) {
13+
if (sqrt(pow(points[j][0] - cx, 2) + pow(points[j][1] - cy, 2)) <= r) {
14+
count++;
15+
}
16+
}
17+
ans[i] = count;
18+
}
19+
*returnSize = queriesSize;
20+
return ans;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn count_points(points: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {
3+
queries
4+
.iter()
5+
.map(|v| {
6+
let cx = v[0];
7+
let cy = v[1];
8+
let r = v[2].pow(2);
9+
let mut count = 0;
10+
for p in points.iter() {
11+
if ((p[0] - cx).pow(2) + (p[1] - cy).pow(2)) <= r {
12+
count += 1;
13+
}
14+
}
15+
count
16+
})
17+
.collect()
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
function countPoints(points: number[][], queries: number[][]): number[] {
2-
let ans = [];
3-
for (let [cx, cy, r] of queries) {
4-
let square = r ** 2;
5-
let count = 0;
6-
for (let [px, py] of points) {
7-
if ((px - cx) ** 2 + (py - cy) ** 2 <= square) {
8-
++count;
2+
return queries.map(([cx, cy, r]) => {
3+
let res = 0;
4+
for (const [px, py] of points) {
5+
if (Math.sqrt((cx - px) ** 2 + (cy - py) ** 2) <= r) {
6+
res++;
97
}
108
}
11-
ans.push(count);
12-
}
13-
return ans;
9+
return res;
10+
});
1411
}

0 commit comments

Comments
 (0)