Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9399197

Browse files
feat: add solutions to lc problems: No.2257,2258 (#1718)
* No.2257.Count Unguarded Cells in the Grid * No.2258.Escape the Spreading Fire
1 parent 95cc2c2 commit 9399197

File tree

10 files changed

+939
-543
lines changed

10 files changed

+939
-543
lines changed

‎solution/2200-2299/2257.Count Unguarded Cells in the Grid/README.md‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
**方法一:模拟**
5757

58-
我们创建一个 $m \times n$ 的二维数组 $g,ドル其中 $g[i][j]$ 表示第 $i$ 行第 $j$ 列的格子。初始时,$g[i][j]$ 的值为 0ドル,ドル表示该格子没有被保卫。
58+
我们创建一个 $m \times n$ 的二维数组 $g,ドル其中 $g[i][j]$ 表示第 $i$ 行第 $j$ 列的格子。初始时$g[i][j]$ 的值为 0ドル,ドル表示该格子没有被保卫。
5959

6060
然后遍历所有的警卫和墙,将 $g[i][j]$ 的值置为 2ドル,ドル这些位置不能被访问。
6161

@@ -204,7 +204,34 @@ func countUnguarded(m int, n int, guards [][]int, walls [][]int) (ans int) {
204204
### **TypeScript**
205205

206206
```ts
207-
207+
function countUnguarded(m: number, n: number, guards: number[][], walls: number[][]): number {
208+
const g: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
209+
for (const [i, j] of guards) {
210+
g[i][j] = 2;
211+
}
212+
for (const [i, j] of walls) {
213+
g[i][j] = 2;
214+
}
215+
const dirs: number[] = [-1, 0, 1, 0, -1];
216+
for (const [i, j] of guards) {
217+
for (let k = 0; k < 4; ++k) {
218+
let [x, y] = [i, j];
219+
let [a, b] = [dirs[k], dirs[k + 1]];
220+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) {
221+
x += a;
222+
y += b;
223+
g[x][y] = 1;
224+
}
225+
}
226+
}
227+
let ans = 0;
228+
for (const row of g) {
229+
for (const v of row) {
230+
ans += v === 0 ? 1 : 0;
231+
}
232+
}
233+
return ans;
234+
}
208235
```
209236

210237
### **...**

‎solution/2200-2299/2257.Count Unguarded Cells in the Grid/README_EN.md‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ There are a total of 4 unguarded cells, so we return 4.
4545

4646
## Solutions
4747

48+
**Solution 1: Simulation**
49+
50+
We create a two-dimensional array $g$ of size $m \times n,ドル where $g[i][j]$ represents the cell in row $i$ and column $j$. Initially, the value of $g[i][j]$ is 0ドル,ドル indicating that the cell is not guarded.
51+
52+
Then, we traverse all guards and walls, and set the value of $g[i][j]$ to 2ドル,ドル indicating that these positions cannot be accessed.
53+
54+
Next, we traverse all guard positions, simulate in four directions from that position until we encounter a wall or guard, or go out of bounds. During the simulation, we set the value of the encountered cell to 1ドル,ドル indicating that the cell is guarded.
55+
56+
Finally, we traverse $g$ and count the number of cells with a value of 0ドル,ドル which is the answer.
57+
58+
The time complexity is $O(m \times n),ドル and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns in the grid, respectively.
59+
4860
<!-- tabs:start -->
4961

5062
### **Python3**
@@ -180,7 +192,34 @@ func countUnguarded(m int, n int, guards [][]int, walls [][]int) (ans int) {
180192
### **TypeScript**
181193

182194
```ts
183-
195+
function countUnguarded(m: number, n: number, guards: number[][], walls: number[][]): number {
196+
const g: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
197+
for (const [i, j] of guards) {
198+
g[i][j] = 2;
199+
}
200+
for (const [i, j] of walls) {
201+
g[i][j] = 2;
202+
}
203+
const dirs: number[] = [-1, 0, 1, 0, -1];
204+
for (const [i, j] of guards) {
205+
for (let k = 0; k < 4; ++k) {
206+
let [x, y] = [i, j];
207+
let [a, b] = [dirs[k], dirs[k + 1]];
208+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) {
209+
x += a;
210+
y += b;
211+
g[x][y] = 1;
212+
}
213+
}
214+
}
215+
let ans = 0;
216+
for (const row of g) {
217+
for (const v of row) {
218+
ans += v === 0 ? 1 : 0;
219+
}
220+
}
221+
return ans;
222+
}
184223
```
185224

186225
### **...**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function countUnguarded(m: number, n: number, guards: number[][], walls: number[][]): number {
2+
const g: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
3+
for (const [i, j] of guards) {
4+
g[i][j] = 2;
5+
}
6+
for (const [i, j] of walls) {
7+
g[i][j] = 2;
8+
}
9+
const dirs: number[] = [-1, 0, 1, 0, -1];
10+
for (const [i, j] of guards) {
11+
for (let k = 0; k < 4; ++k) {
12+
let [x, y] = [i, j];
13+
let [a, b] = [dirs[k], dirs[k + 1]];
14+
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] < 2) {
15+
x += a;
16+
y += b;
17+
g[x][y] = 1;
18+
}
19+
}
20+
}
21+
let ans = 0;
22+
for (const row of g) {
23+
for (const v of row) {
24+
ans += v === 0 ? 1 : 0;
25+
}
26+
}
27+
return ans;
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /