Skip to content

Commit 4444859

Browse files
authored
Merge pull request #1101 from 0xff-dev/1765
Add solution and test-cases for problem 1765
2 parents c5ac5cc + 11e68c1 commit 4444859

File tree

5 files changed

+72
-23
lines changed

5 files changed

+72
-23
lines changed
Loading
Loading

leetcode/1701-1800/1765.Map-of-Highest-Peak/README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [1765.Map of Highest Peak][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer matrix `isWater` of size `m x n` that represents a map of **land** and **water** cells.
5+
6+
- If `isWater[i][j] == 0`, cell `(i, j)` is a **land** cell.
7+
- If `isWater[i][j] == 1`, cell `(i, j)` is a **water** cell.
8+
9+
You must assign each cell a height in a way that follows these rules:
10+
11+
- The height of each cell must be non-negative.
12+
- If the cell is a **water** cell, its height must be `0`.
13+
- Any two adjacent cells must have an absolute height difference of **at most 1**. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).
14+
15+
Find an assignment of heights such that the maximum height in the matrix is **maximized**.
716

8-
**Example 1:**
17+
Return an integer matrix `height` of size `m x n` where `height[i][j]` is cell `(i, j)`'s height. If there are multiple solutions, return **any** of them.
18+
19+
**Example 1:**
20+
21+
![1](./1.png)
922

1023
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
24+
Input: isWater = [[0,1],[0,0]]
25+
Output: [[1,0],[2,1]]
26+
Explanation: The image shows the assigned heights of each cell.
27+
The blue cell is the water cell, and the green cells are the land cells.
1328
```
1429

15-
## 题意
16-
> ...
30+
**Example 2:**
1731

18-
## 题解
32+
![2](./2.png)
1933

20-
### 思路1
21-
> ...
22-
Map of Highest Peak
23-
```go
2434
```
25-
35+
Input: isWater = [[0,0,1],[1,0,0],[0,0,0]]
36+
Output: [[1,1,0],[0,1,1],[1,2,2]]
37+
Explanation: A height of 2 is the maximum possible height of any assignment.
38+
Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.
39+
```
2640

2741
## 结语
2842

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(isWater [][]int) [][]int {
4+
var dir = [][2]int{
5+
{0, 1}, {0, -1}, {1, 0}, {-1, 0},
6+
}
7+
queue := [][2]int{}
8+
m, n := len(isWater), len(isWater[0])
9+
res := make([][]int, m)
10+
for i := range m {
11+
res[i] = make([]int, n)
12+
for j := range n {
13+
res[i][j] = -1
14+
}
15+
}
16+
for i := range m {
17+
for j := range n {
18+
if isWater[i][j] == 1 {
19+
queue = append(queue, [2]int{i, j})
20+
res[i][j] = 0
21+
}
22+
}
23+
}
24+
h := 1
25+
for len(queue) > 0 {
26+
nq := make([][2]int, 0)
27+
for _, cur := range queue {
28+
for _, d := range dir {
29+
nx, ny := cur[0]+d[0], cur[1]+d[1]
30+
if nx >= 0 && nx < m && ny >= 0 && ny < n && res[nx][ny] == -1 {
31+
res[nx][ny] = h
32+
nq = append(nq, [2]int{nx, ny})
33+
}
34+
}
35+
}
36+
queue = nq
37+
h++
38+
}
39+
40+
return res
541
}

leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{0, 1}, {0, 0}}, [][]int{{1, 0}, {2, 1}}},
17+
{"TestCase2", [][]int{{0, 0, 1}, {1, 0, 0}, {0, 0, 0}}, [][]int{{1, 1, 0}, {0, 1, 1}, {1, 2, 2}}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)