Skip to content

Commit 14ccd5c

Browse files
committed
feat: add solutions to lc problem: No.1765
No.1765.Map of Highest Peak
1 parent 2334e9a commit 14ccd5c

File tree

6 files changed

+388
-2
lines changed

6 files changed

+388
-2
lines changed

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

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,154 @@
6565

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

68+
多源 BFS。
69+
6870
<!-- tabs:start -->
6971

7072
### **Python3**
7173

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

7476
```python
75-
77+
class Solution:
78+
def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
79+
m, n = len(isWater), len(isWater[0])
80+
ans = [[-1] * n for _ in range(m)]
81+
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
86+
q.append((i, j))
87+
while q:
88+
i, j = q.popleft()
89+
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
90+
x, y = i + a, j + b
91+
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
92+
ans[x][y] = ans[i][j] + 1
93+
q.append((x, y))
94+
return ans
7695
```
7796

7897
### **Java**
7998

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

82101
```java
102+
class Solution {
103+
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
104+
105+
public int[][] highestPeak(int[][] isWater) {
106+
int m = isWater.length, n = isWater[0].length;
107+
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<>();
112+
for (int i = 0; i < m; ++i) {
113+
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});
117+
}
118+
}
119+
}
120+
while (!q.isEmpty()) {
121+
int[] p = q.pollFirst();
122+
int i = p[0], j = p[1];
123+
for (int[] dir : dirs) {
124+
int x = i + dir[0], y = j + dir[1];
125+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
126+
ans[x][y] = ans[i][j] + 1;
127+
q.offerLast(new int[]{x, y});
128+
}
129+
}
130+
}
131+
return ans;
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
typedef pair<int, int> PII;
140+
141+
class Solution {
142+
public:
143+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
144+
145+
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
146+
int m = isWater.size(), n = isWater[0].size();
147+
vector<vector<int>> ans(m, vector<int>(n, -1));
148+
queue<PII> q;
149+
for (int i = 0; i < m; ++i)
150+
{
151+
for (int j = 0; j < n; ++j)
152+
{
153+
if (isWater[i][j] == 1)
154+
{
155+
ans[i][j] = 0;
156+
q.push({i, j});
157+
}
158+
}
159+
}
160+
while (!q.empty())
161+
{
162+
PII p = q.front();
163+
q.pop();
164+
int i = p.first, j = p.second;
165+
for (auto& dir : dirs)
166+
{
167+
int x = i + dir[0], y = j + dir[1];
168+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1)
169+
{
170+
ans[x][y] = ans[i][j] + 1;
171+
q.push({x, y});
172+
}
173+
}
174+
}
175+
return ans;
176+
}
177+
};
178+
```
83179

180+
### **Go**
181+
182+
```go
183+
func highestPeak(isWater [][]int) [][]int {
184+
m, n := len(isWater), len(isWater[0])
185+
ans := make([][]int, m)
186+
for i := range ans {
187+
ans[i] = make([]int, n)
188+
for j := range ans[i] {
189+
ans[i][j] = -1
190+
}
191+
}
192+
type pair struct{ i, j int }
193+
var q []pair
194+
for i := 0; i < m; i++ {
195+
for j := 0; j < n; j++ {
196+
if isWater[i][j] == 1 {
197+
ans[i][j] = 0
198+
q = append(q, pair{i, j})
199+
}
200+
}
201+
}
202+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
203+
for len(q) > 0 {
204+
p := q[0]
205+
q = q[1:]
206+
for _, dir := range dirs {
207+
x, y := p.i+dir[0], p.j+dir[1]
208+
if x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 {
209+
ans[x][y] = ans[p.i][p.j] + 1
210+
q = append(q, pair{x, y})
211+
}
212+
}
213+
}
214+
return ans
215+
}
84216
```
85217

86218
### **...**

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

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,150 @@ Any height assignment that has a maximum height of 2 while still meeting the rul
5959

6060
## Solutions
6161

62+
BFS.
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

6668
```python
67-
69+
class Solution:
70+
def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
71+
m, n = len(isWater), len(isWater[0])
72+
ans = [[-1] * n for _ in range(m)]
73+
q = deque()
74+
for i in range(m):
75+
for j in range(n):
76+
if isWater[i][j] == 1:
77+
ans[i][j] = 0
78+
q.append((i, j))
79+
while q:
80+
i, j = q.popleft()
81+
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
82+
x, y = i + a, j + b
83+
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
84+
ans[x][y] = ans[i][j] + 1
85+
q.append((x, y))
86+
return ans
6887
```
6988

7089
### **Java**
7190

7291
```java
92+
class Solution {
93+
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
94+
95+
public int[][] highestPeak(int[][] isWater) {
96+
int m = isWater.length, n = isWater[0].length;
97+
int[][] ans = new int[m][n];
98+
for (int i = 0; i < m; ++i) {
99+
Arrays.fill(ans[i], -1);
100+
}
101+
Deque<int[]> q = new LinkedList<>();
102+
for (int i = 0; i < m; ++i) {
103+
for (int j = 0; j < n; ++j) {
104+
if (isWater[i][j] == 1) {
105+
ans[i][j] = 0;
106+
q.offerLast(new int[]{i, j});
107+
}
108+
}
109+
}
110+
while (!q.isEmpty()) {
111+
int[] p = q.pollFirst();
112+
int i = p[0], j = p[1];
113+
for (int[] dir : dirs) {
114+
int x = i + dir[0], y = j + dir[1];
115+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
116+
ans[x][y] = ans[i][j] + 1;
117+
q.offerLast(new int[]{x, y});
118+
}
119+
}
120+
}
121+
return ans;
122+
}
123+
}
124+
```
125+
126+
### **C++**
127+
128+
```cpp
129+
typedef pair<int, int> PII;
130+
131+
class Solution {
132+
public:
133+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
134+
135+
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
136+
int m = isWater.size(), n = isWater[0].size();
137+
vector<vector<int>> ans(m, vector<int>(n, -1));
138+
queue<PII> q;
139+
for (int i = 0; i < m; ++i)
140+
{
141+
for (int j = 0; j < n; ++j)
142+
{
143+
if (isWater[i][j] == 1)
144+
{
145+
ans[i][j] = 0;
146+
q.push({i, j});
147+
}
148+
}
149+
}
150+
while (!q.empty())
151+
{
152+
PII p = q.front();
153+
q.pop();
154+
int i = p.first, j = p.second;
155+
for (auto& dir : dirs)
156+
{
157+
int x = i + dir[0], y = j + dir[1];
158+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1)
159+
{
160+
ans[x][y] = ans[i][j] + 1;
161+
q.push({x, y});
162+
}
163+
}
164+
}
165+
return ans;
166+
}
167+
};
168+
```
73169

170+
### **Go**
171+
172+
```go
173+
func highestPeak(isWater [][]int) [][]int {
174+
m, n := len(isWater), len(isWater[0])
175+
ans := make([][]int, m)
176+
for i := range ans {
177+
ans[i] = make([]int, n)
178+
for j := range ans[i] {
179+
ans[i][j] = -1
180+
}
181+
}
182+
type pair struct{ i, j int }
183+
var q []pair
184+
for i := 0; i < m; i++ {
185+
for j := 0; j < n; j++ {
186+
if isWater[i][j] == 1 {
187+
ans[i][j] = 0
188+
q = append(q, pair{i, j})
189+
}
190+
}
191+
}
192+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
193+
for len(q) > 0 {
194+
p := q[0]
195+
q = q[1:]
196+
for _, dir := range dirs {
197+
x, y := p.i+dir[0], p.j+dir[1]
198+
if x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 {
199+
ans[x][y] = ans[p.i][p.j] + 1
200+
q = append(q, pair{x, y})
201+
}
202+
}
203+
}
204+
return ans
205+
}
74206
```
75207

76208
### **...**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
typedef pair<int, int> PII;
2+
3+
class Solution {
4+
public:
5+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
6+
7+
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
8+
int m = isWater.size(), n = isWater[0].size();
9+
vector<vector<int>> ans(m, vector<int>(n, -1));
10+
queue<PII> q;
11+
for (int i = 0; i < m; ++i)
12+
{
13+
for (int j = 0; j < n; ++j)
14+
{
15+
if (isWater[i][j] == 1)
16+
{
17+
ans[i][j] = 0;
18+
q.push({i, j});
19+
}
20+
}
21+
}
22+
while (!q.empty())
23+
{
24+
PII p = q.front();
25+
q.pop();
26+
int i = p.first, j = p.second;
27+
for (auto& dir : dirs)
28+
{
29+
int x = i + dir[0], y = j + dir[1];
30+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1)
31+
{
32+
ans[x][y] = ans[i][j] + 1;
33+
q.push({x, y});
34+
}
35+
}
36+
}
37+
return ans;
38+
}
39+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func highestPeak(isWater [][]int) [][]int {
2+
m, n := len(isWater), len(isWater[0])
3+
ans := make([][]int, m)
4+
for i := range ans {
5+
ans[i] = make([]int, n)
6+
for j := range ans[i] {
7+
ans[i][j] = -1
8+
}
9+
}
10+
type pair struct{ i, j int }
11+
var q []pair
12+
for i := 0; i < m; i++ {
13+
for j := 0; j < n; j++ {
14+
if isWater[i][j] == 1 {
15+
ans[i][j] = 0
16+
q = append(q, pair{i, j})
17+
}
18+
}
19+
}
20+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
21+
for len(q) > 0 {
22+
p := q[0]
23+
q = q[1:]
24+
for _, dir := range dirs {
25+
x, y := p.i+dir[0], p.j+dir[1]
26+
if x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 {
27+
ans[x][y] = ans[p.i][p.j] + 1
28+
q = append(q, pair{x, y})
29+
}
30+
}
31+
}
32+
return ans
33+
}

0 commit comments

Comments
 (0)