Skip to content

Commit ae8cb2d

Browse files
committed
feat: add typescript solution to lc problem: No.2249
No.2249.Count Lattice Points Inside a Circle
1 parent a5eae46 commit ae8cb2d

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,32 @@
7979
### **TypeScript**
8080

8181
```ts
82-
82+
function countLatticePoints(circles: number[][]): number {
83+
const n = circles.length;
84+
let minX = Number.MAX_SAFE_INTEGER, minY = minX,
85+
maxX = Number.MIN_SAFE_INTEGER, maxY = maxX;
86+
let squares = [];
87+
for (let [x, y, r] of circles) {
88+
minX = Math.min(x - r, minX);
89+
minY = Math.min(y - r, minY);
90+
maxX = Math.max(x + r, maxX);
91+
maxY = Math.max(y + r, maxY);
92+
squares.push(r ** 2);
93+
}
94+
let ans = 0;
95+
for (let i = minX; i <= maxX; i++) {
96+
for (let j = minY; j <= maxY; j++) {
97+
for (let k = 0; k < n; k++) {
98+
const [x, y, ] = circles[k];
99+
if ((i - x) ** 2 + (j - y) ** 2 <= squares[k]) {
100+
ans++;
101+
break;
102+
}
103+
}
104+
}
105+
}
106+
return ans;
107+
};
83108
```
84109

85110
### **...**

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,32 @@ Some of them are (0, 2), (2, 0), (2, 4), (3, 2), and (4, 4).
6565
### **TypeScript**
6666

6767
```ts
68-
68+
function countLatticePoints(circles: number[][]): number {
69+
const n = circles.length;
70+
let minX = Number.MAX_SAFE_INTEGER, minY = minX,
71+
maxX = Number.MIN_SAFE_INTEGER, maxY = maxX;
72+
let squares = [];
73+
for (let [x, y, r] of circles) {
74+
minX = Math.min(x - r, minX);
75+
minY = Math.min(y - r, minY);
76+
maxX = Math.max(x + r, maxX);
77+
maxY = Math.max(y + r, maxY);
78+
squares.push(r ** 2);
79+
}
80+
let ans = 0;
81+
for (let i = minX; i <= maxX; i++) {
82+
for (let j = minY; j <= maxY; j++) {
83+
for (let k = 0; k < n; k++) {
84+
const [x, y, ] = circles[k];
85+
if ((i - x) ** 2 + (j - y) ** 2 <= squares[k]) {
86+
ans++;
87+
break;
88+
}
89+
}
90+
}
91+
}
92+
return ans;
93+
};
6994
```
7095

7196
### **...**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function countLatticePoints(circles: number[][]): number {
2+
const n = circles.length;
3+
let minX = Number.MAX_SAFE_INTEGER, minY = minX,
4+
maxX = Number.MIN_SAFE_INTEGER, maxY = maxX;
5+
let squares = [];
6+
for (let [x, y, r] of circles) {
7+
minX = Math.min(x - r, minX);
8+
minY = Math.min(y - r, minY);
9+
maxX = Math.max(x + r, maxX);
10+
maxY = Math.max(y + r, maxY);
11+
squares.push(r ** 2);
12+
}
13+
let ans = 0;
14+
for (let i = minX; i <= maxX; i++) {
15+
for (let j = minY; j <= maxY; j++) {
16+
for (let k = 0; k < n; k++) {
17+
const [x, y, ] = circles[k];
18+
if ((i - x) ** 2 + (j - y) ** 2 <= squares[k]) {
19+
ans++;
20+
break;
21+
}
22+
}
23+
}
24+
}
25+
return ans;
26+
};

0 commit comments

Comments
 (0)