Skip to content

Commit b0710ef

Browse files
committed
feat: add ts solution to lc problem: No.1765
No.1765.Map of Highest Peak
1 parent 082f098 commit b0710ef

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

solution/1700-1799/1765.Map of Highest Peak/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,42 @@ class Solution {
133133
}
134134
```
135135

136+
### **TypeScript**
137+
138+
```ts
139+
function highestPeak(isWater: number[][]): number[][] {
140+
const m = isWater.length, n = isWater[0].length;
141+
let ans: Array<Array<number>> = Array.from({ length: m }, v => new Array(n).fill(-1));
142+
// BFS
143+
let queue: Array<Array<number>> = []; // i, j, num
144+
for (let i = 0; i < m; i++) {
145+
for (let j = 0; j < n; j++) {
146+
if (isWater[i][j]) {
147+
ans[i][j] = 0;
148+
queue.push([i, j, 0]);
149+
}
150+
}
151+
}
152+
const directions = [[0, -1], [-1, 0], [0, 1], [1, 0]]; // left, up, right, down
153+
while (queue.length) {
154+
// 消除push/shift出现超时问题
155+
let tmp: Array<Array<number>> = [];
156+
for (const [i, j, num] of queue) {
157+
for (const [dx, dy] of directions) {
158+
const x = i + dx, y = j + dy;
159+
// 校验合法的相邻格子
160+
if (x > -1 && x < m && y > -1 && y < n && ans[x][y] == -1) {
161+
ans[x][y] = num + 1;
162+
tmp.push([x, y, num + 1]);
163+
}
164+
}
165+
}
166+
queue = tmp;
167+
}
168+
return ans;
169+
};
170+
```
171+
136172
### **C++**
137173

138174
```cpp

solution/1700-1799/1765.Map of Highest Peak/README_EN.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,42 @@ class Solution {
123123
}
124124
```
125125

126+
### **TypeScript**
127+
128+
```ts
129+
function highestPeak(isWater: number[][]): number[][] {
130+
const m = isWater.length, n = isWater[0].length;
131+
let ans: Array<Array<number>> = Array.from({ length: m }, v => new Array(n).fill(-1));
132+
// BFS
133+
let queue: Array<Array<number>> = []; // i, j, num
134+
for (let i = 0; i < m; i++) {
135+
for (let j = 0; j < n; j++) {
136+
if (isWater[i][j]) {
137+
ans[i][j] = 0;
138+
queue.push([i, j, 0]);
139+
}
140+
}
141+
}
142+
const directions = [[0, -1], [-1, 0], [0, 1], [1, 0]]; // left, up, right, down
143+
while (queue.length) {
144+
// 消除push/shift出现超时问题
145+
let tmp: Array<Array<number>> = [];
146+
for (const [i, j, num] of queue) {
147+
for (const [dx, dy] of directions) {
148+
const x = i + dx, y = j + dy;
149+
// 校验合法的相邻格子
150+
if (x > -1 && x < m && y > -1 && y < n && ans[x][y] == -1) {
151+
ans[x][y] = num + 1;
152+
tmp.push([x, y, num + 1]);
153+
}
154+
}
155+
}
156+
queue = tmp;
157+
}
158+
return ans;
159+
};
160+
```
161+
126162
### **C++**
127163

128164
```cpp
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function highestPeak(isWater: number[][]): number[][] {
2+
const m = isWater.length, n = isWater[0].length;
3+
let ans: Array<Array<number>> = Array.from({ length: m }, v => new Array(n).fill(-1));
4+
// BFS
5+
let queue: Array<Array<number>> = []; // i, j, num
6+
for (let i = 0; i < m; i++) {
7+
for (let j = 0; j < n; j++) {
8+
if (isWater[i][j]) {
9+
ans[i][j] = 0;
10+
queue.push([i, j, 0]);
11+
}
12+
}
13+
}
14+
const directions = [[0, -1], [-1, 0], [0, 1], [1, 0]]; // left, up, right, down
15+
while (queue.length) {
16+
// 消除push/shift出现超时问题
17+
let tmp: Array<Array<number>> = [];
18+
for (const [i, j, num] of queue) {
19+
for (const [dx, dy] of directions) {
20+
const x = i + dx, y = j + dy;
21+
// 校验合法的相邻格子
22+
if (x > -1 && x < m && y > -1 && y < n && ans[x][y] == -1) {
23+
ans[x][y] = num + 1;
24+
tmp.push([x, y, num + 1]);
25+
}
26+
}
27+
}
28+
queue = tmp;
29+
}
30+
return ans;
31+
};

0 commit comments

Comments
 (0)