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 0c87249

Browse files
feat: add ts solution to lc problem: No.1162
No.1162.As Far from Land as Possible
1 parent 017635f commit 0c87249

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

‎solution/1100-1199/1162.As Far from Land as Possible/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,39 @@ class Solution {
114114
}
115115
}
116116
```
117+
### **TypeScript**
118+
119+
```ts
120+
function maxDistance(grid: number[][]): number {
121+
const m = grid.length, n = grid[0].length;
122+
let queue: Array<Array<number>> = [];
123+
for (let i = 0; i < m; i++) {
124+
for (let j = 0; j < n; j++) {
125+
if (grid[i][j]) {
126+
queue.push([i, j]);
127+
}
128+
}
129+
}
130+
if ([0, m * n].includes(queue.length)) return -1;
131+
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]];
132+
let depth = 1;
133+
while (queue.length) {
134+
depth += 1;
135+
let nextLevel: Array<Array<number>> = [];
136+
for (let [x, y] of queue) {
137+
for (let [dx, dy] of directions) {
138+
const [i, j] = [x + dx, y + dy];
139+
if (i >= 0 && i < m && j >= 0 && j < n && !grid[i][j]) {
140+
grid[i][j] = depth;
141+
nextLevel.push([i, j]);
142+
}
143+
}
144+
}
145+
queue = nextLevel;
146+
}
147+
return depth - 2;
148+
};
149+
```
117150

118151
### **C++**
119152

‎solution/1100-1199/1162.As Far from Land as Possible/README_EN.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,39 @@ class Solution {
100100
}
101101
}
102102
```
103+
### **TypeScript**
104+
105+
```ts
106+
function maxDistance(grid: number[][]): number {
107+
const m = grid.length, n = grid[0].length;
108+
let queue: Array<Array<number>> = [];
109+
for (let i = 0; i < m; i++) {
110+
for (let j = 0; j < n; j++) {
111+
if (grid[i][j]) {
112+
queue.push([i, j]);
113+
}
114+
}
115+
}
116+
if ([0, m * n].includes(queue.length)) return -1;
117+
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]];
118+
let depth = 1;
119+
while (queue.length) {
120+
depth += 1;
121+
let nextLevel: Array<Array<number>> = [];
122+
for (let [x, y] of queue) {
123+
for (let [dx, dy] of directions) {
124+
const [i, j] = [x + dx, y + dy];
125+
if (i >= 0 && i < m && j >= 0 && j < n && !grid[i][j]) {
126+
grid[i][j] = depth;
127+
nextLevel.push([i, j]);
128+
}
129+
}
130+
}
131+
queue = nextLevel;
132+
}
133+
return depth - 2;
134+
};
135+
```
103136

104137
### **C++**
105138

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function maxDistance(grid: number[][]): number {
2+
const m = grid.length, n = grid[0].length;
3+
let queue: Array<Array<number>> = [];
4+
for (let i = 0; i < m; i++) {
5+
for (let j = 0; j < n; j++) {
6+
if (grid[i][j]) {
7+
queue.push([i, j]);
8+
}
9+
}
10+
}
11+
if ([0, m * n].includes(queue.length)) return -1;
12+
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]];
13+
let depth = 1;
14+
while (queue.length) {
15+
depth += 1;
16+
let nextLevel: Array<Array<number>> = [];
17+
for (let [x, y] of queue) {
18+
for (let [dx, dy] of directions) {
19+
const [i, j] = [x + dx, y + dy];
20+
if (i >= 0 && i < m && j >= 0 && j < n && !grid[i][j]) {
21+
grid[i][j] = depth;
22+
nextLevel.push([i, j]);
23+
}
24+
}
25+
}
26+
queue = nextLevel;
27+
}
28+
return depth - 2;
29+
};

0 commit comments

Comments
(0)

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