Skip to content

Commit 50eaafc

Browse files
committed
feat: add solutions to lc problem: No.1765
No.1765.Map of Highest Peak
1 parent d667967 commit 50eaafc

File tree

8 files changed

+437
-242
lines changed

8 files changed

+437
-242
lines changed

solution/1700-1799/1764.Form Array by Concatenating Subarrays of Another Array/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
如果 $i$ 走到了 $groups.length$,说明所有的子数组都匹配上了,返回 `true`,否则返回 `false`
6767

68-
时间复杂度 $O(n \times m),空间复杂度 O(1)$。其中 $n$ 和 $m$ 分别为 `groups``nums` 的长度。
68+
时间复杂度 $O(n \times m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为 `groups``nums` 的长度。
6969

7070
<!-- tabs:start -->
7171

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

Lines changed: 194 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68-
多源 BFS。
68+
**方法一:BFS**
69+
70+
根据题目描述,水域的高度必须是 $0$,而任意相邻格子的高度差至多为 $1$。因此,我们可以从所有水域格子出发,用 BFS 搜索相邻且未访问过的格子,将其高度置为当前格子的高度再加一。
71+
72+
最后返回结果矩阵即可。
73+
74+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是整数矩阵 `isWater` 的行数和列数。
6975

7076
<!-- tabs:start -->
7177

@@ -79,52 +85,70 @@ class Solution:
7985
m, n = len(isWater), len(isWater[0])
8086
ans = [[-1] * n for _ in range(m)]
8187
q = deque()
82-
for i in range(m):
83-
for j in range(n):
84-
if isWater[i][j] == 1:
85-
ans[i][j] = 0
88+
for i, row in enumerate(isWater):
89+
for j, v in enumerate(row):
90+
if v:
8691
q.append((i, j))
92+
ans[i][j] = 0
8793
while q:
8894
i, j = q.popleft()
89-
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
95+
for a, b in pairwise((-1, 0, 1, 0, -1)):
9096
x, y = i + a, j + b
9197
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
9298
ans[x][y] = ans[i][j] + 1
9399
q.append((x, y))
94100
return ans
95101
```
96102

103+
```python
104+
class Solution:
105+
def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
106+
m, n = len(isWater), len(isWater[0])
107+
ans = [[-1] * n for _ in range(m)]
108+
q = deque()
109+
for i, row in enumerate(isWater):
110+
for j, v in enumerate(row):
111+
if v:
112+
q.append((i, j))
113+
ans[i][j] = 0
114+
while q:
115+
for _ in range(len(q)):
116+
i, j = q.popleft()
117+
for a, b in pairwise((-1, 0, 1, 0, -1)):
118+
x, y = i + a, j + b
119+
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
120+
ans[x][y] = ans[i][j] + 1
121+
q.append((x, y))
122+
return ans
123+
```
124+
97125
### **Java**
98126

99127
<!-- 这里可写当前语言的特殊实现逻辑 -->
100128

101129
```java
102130
class Solution {
103-
private int[][] dirs = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
104-
105131
public int[][] highestPeak(int[][] isWater) {
106132
int m = isWater.length, n = isWater[0].length;
107133
int[][] ans = new int[m][n];
108-
for (int i = 0; i < m; ++i) {
109-
Arrays.fill(ans[i], -1);
110-
}
111-
Deque<int[]> q = new LinkedList<>();
134+
Deque<int[]> q = new ArrayDeque<>();
112135
for (int i = 0; i < m; ++i) {
113136
for (int j = 0; j < n; ++j) {
114-
if (isWater[i][j] == 1) {
115-
ans[i][j] = 0;
116-
q.offerLast(new int[] {i, j});
137+
ans[i][j] = isWater[i][j] - 1;
138+
if (ans[i][j] == 0) {
139+
q.offer(new int[] {i, j});
117140
}
118141
}
119142
}
143+
int[] dirs = {-1, 0, 1, 0, -1};
120144
while (!q.isEmpty()) {
121-
int[] p = q.pollFirst();
145+
var p = q.poll();
122146
int i = p[0], j = p[1];
123-
for (int[] dir : dirs) {
124-
int x = i + dir[0], y = j + dir[1];
147+
for (int k = 0; k < 4; ++k) {
148+
int x = i + dirs[k], y = j + dirs[k + 1];
125149
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
126150
ans[x][y] = ans[i][j] + 1;
127-
q.offerLast(new int[] {x, y});
151+
q.offer(new int[] {x, y});
128152
}
129153
}
130154
}
@@ -133,84 +157,106 @@ class Solution {
133157
}
134158
```
135159

136-
### **TypeScript**
137-
138-
```ts
139-
function highestPeak(isWater: number[][]): number[][] {
140-
const m = isWater.length,
141-
n = isWater[0].length;
142-
let ans: Array<Array<number>> = Array.from({ length: m }, v =>
143-
new Array(n).fill(-1),
144-
);
145-
// BFS
146-
let queue: Array<Array<number>> = []; // i, j, num
147-
for (let i = 0; i < m; i++) {
148-
for (let j = 0; j < n; j++) {
149-
if (isWater[i][j]) {
150-
ans[i][j] = 0;
151-
queue.push([i, j, 0]);
160+
```java
161+
class Solution {
162+
public int[][] highestPeak(int[][] isWater) {
163+
int m = isWater.length, n = isWater[0].length;
164+
int[][] ans = new int[m][n];
165+
Deque<int[]> q = new ArrayDeque<>();
166+
for (int i = 0; i < m; ++i) {
167+
for (int j = 0; j < n; ++j) {
168+
ans[i][j] = isWater[i][j] - 1;
169+
if (ans[i][j] == 0) {
170+
q.offer(new int[] {i, j});
171+
}
152172
}
153173
}
154-
}
155-
const directions = [
156-
[0, -1],
157-
[-1, 0],
158-
[0, 1],
159-
[1, 0],
160-
]; // left, up, right, down
161-
while (queue.length) {
162-
// 消除push/shift出现超时问题
163-
let tmp: Array<Array<number>> = [];
164-
for (const [i, j, num] of queue) {
165-
for (const [dx, dy] of directions) {
166-
const x = i + dx,
167-
y = j + dy;
168-
// 校验合法的相邻格子
169-
if (x > -1 && x < m && y > -1 && y < n && ans[x][y] == -1) {
170-
ans[x][y] = num + 1;
171-
tmp.push([x, y, num + 1]);
174+
int[] dirs = {-1, 0, 1, 0, -1};
175+
while (!q.isEmpty()) {
176+
for (int t = q.size(); t > 0; --t) {
177+
var p = q.poll();
178+
int i = p[0], j = p[1];
179+
for (int k = 0; k < 4; ++k) {
180+
int x = i + dirs[k], y = j + dirs[k + 1];
181+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
182+
ans[x][y] = ans[i][j] + 1;
183+
q.offer(new int[] {x, y});
184+
}
172185
}
173186
}
187+
174188
}
175-
queue = tmp;
189+
return ans;
176190
}
177-
return ans;
178191
}
179192
```
180193

181194
### **C++**
182195

183196
```cpp
184-
typedef pair<int, int> PII;
185-
186197
class Solution {
187198
public:
188-
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
199+
const int dirs[5] = {-1, 0, 1, 0, -1};
189200

190201
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
191202
int m = isWater.size(), n = isWater[0].size();
192-
vector<vector<int>> ans(m, vector<int>(n, -1));
193-
queue<PII> q;
203+
vector<vector<int>> ans(m, vector<int>(n));
204+
queue<pair<int, int>> q;
194205
for (int i = 0; i < m; ++i) {
195206
for (int j = 0; j < n; ++j) {
196-
if (isWater[i][j] == 1) {
197-
ans[i][j] = 0;
198-
q.push({i, j});
207+
ans[i][j] = isWater[i][j] - 1;
208+
if (ans[i][j] == 0) {
209+
q.emplace(i, j);
199210
}
200211
}
201212
}
202213
while (!q.empty()) {
203-
PII p = q.front();
214+
auto [i, j] = q.front();
204215
q.pop();
205-
int i = p.first, j = p.second;
206-
for (auto& dir : dirs) {
207-
int x = i + dir[0], y = j + dir[1];
216+
for (int k = 0; k < 4; ++k) {
217+
int x = i + dirs[k], y = j + dirs[k + 1];
208218
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
209219
ans[x][y] = ans[i][j] + 1;
210-
q.push({x, y});
220+
q.emplace(x, y);
221+
}
222+
}
223+
}
224+
return ans;
225+
}
226+
};
227+
```
228+
229+
```cpp
230+
class Solution {
231+
public:
232+
const int dirs[5] = {-1, 0, 1, 0, -1};
233+
234+
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
235+
int m = isWater.size(), n = isWater[0].size();
236+
vector<vector<int>> ans(m, vector<int>(n));
237+
queue<pair<int, int>> q;
238+
for (int i = 0; i < m; ++i) {
239+
for (int j = 0; j < n; ++j) {
240+
ans[i][j] = isWater[i][j] - 1;
241+
if (ans[i][j] == 0) {
242+
q.emplace(i, j);
211243
}
212244
}
213245
}
246+
while (!q.empty()) {
247+
for (int t = q.size(); t; --t) {
248+
auto [i, j] = q.front();
249+
q.pop();
250+
for (int k = 0; k < 4; ++k) {
251+
int x = i + dirs[k], y = j + dirs[k + 1];
252+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
253+
ans[x][y] = ans[i][j] + 1;
254+
q.emplace(x, y);
255+
}
256+
}
257+
}
258+
259+
}
214260
return ans;
215261
}
216262
};
@@ -222,30 +268,26 @@ public:
222268
func highestPeak(isWater [][]int) [][]int {
223269
m, n := len(isWater), len(isWater[0])
224270
ans := make([][]int, m)
225-
for i := range ans {
226-
ans[i] = make([]int, n)
227-
for j := range ans[i] {
228-
ans[i][j] = -1
229-
}
230-
}
231271
type pair struct{ i, j int }
232-
var q []pair
233-
for i := 0; i < m; i++ {
234-
for j := 0; j < n; j++ {
235-
if isWater[i][j] == 1 {
236-
ans[i][j] = 0
272+
q := []pair{}
273+
for i, row := range isWater {
274+
ans[i] = make([]int, n)
275+
for j, v := range row {
276+
ans[i][j] = v - 1
277+
if v == 1 {
237278
q = append(q, pair{i, j})
238279
}
239280
}
240281
}
241-
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
282+
dirs := []int{-1, 0, 1, 0, -1}
242283
for len(q) > 0 {
243284
p := q[0]
244285
q = q[1:]
245-
for _, dir := range dirs {
246-
x, y := p.i+dir[0], p.j+dir[1]
286+
i, j := p.i, p.j
287+
for k := 0; k < 4; k++ {
288+
x, y := i+dirs[k], j+dirs[k+1]
247289
if x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 {
248-
ans[x][y] = ans[p.i][p.j] + 1
290+
ans[x][y] = ans[i][j] + 1
249291
q = append(q, pair{x, y})
250292
}
251293
}
@@ -254,6 +296,75 @@ func highestPeak(isWater [][]int) [][]int {
254296
}
255297
```
256298

299+
```go
300+
func highestPeak(isWater [][]int) [][]int {
301+
m, n := len(isWater), len(isWater[0])
302+
ans := make([][]int, m)
303+
type pair struct{ i, j int }
304+
q := []pair{}
305+
for i, row := range isWater {
306+
ans[i] = make([]int, n)
307+
for j, v := range row {
308+
ans[i][j] = v - 1
309+
if v == 1 {
310+
q = append(q, pair{i, j})
311+
}
312+
}
313+
}
314+
dirs := []int{-1, 0, 1, 0, -1}
315+
for len(q) > 0 {
316+
for t := len(q); t > 0; t-- {
317+
p := q[0]
318+
q = q[1:]
319+
i, j := p.i, p.j
320+
for k := 0; k < 4; k++ {
321+
x, y := i+dirs[k], j+dirs[k+1]
322+
if x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 {
323+
ans[x][y] = ans[i][j] + 1
324+
q = append(q, pair{x, y})
325+
}
326+
}
327+
}
328+
}
329+
return ans
330+
}
331+
```
332+
333+
### **TypeScript**
334+
335+
```ts
336+
function highestPeak(isWater: number[][]): number[][] {
337+
const m = isWater.length;
338+
const n = isWater[0].length;
339+
let ans: number[][] = [];
340+
let q: number[][] = [];
341+
for (let i = 0; i < m; ++i) {
342+
ans.push(new Array(n).fill(-1));
343+
for (let j = 0; j < n; ++j) {
344+
if (isWater[i][j]) {
345+
q.push([i, j]);
346+
ans[i][j] = 0;
347+
}
348+
}
349+
}
350+
const dirs = [-1, 0, 1, 0, -1];
351+
while (q.length) {
352+
let tq: number[][] = [];
353+
for (const [i, j] of q) {
354+
for (let k = 0; k < 4; k++) {
355+
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
356+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
357+
tq.push([x, y]);
358+
ans[x][y] = ans[i][j] + 1;
359+
}
360+
}
361+
}
362+
q = tq;
363+
}
364+
return ans;
365+
}
366+
```
367+
257368
### **...**
258369

259370
```

0 commit comments

Comments
 (0)