Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/1701-1800/1765.Map-of-Highest-Peak/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 28 additions & 14 deletions leetcode/1701-1800/1765.Map-of-Highest-Peak/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
# [1765.Map of Highest Peak][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an integer matrix `isWater` of size `m x n` that represents a map of **land** and **water** cells.

- If `isWater[i][j] == 0`, cell `(i, j)` is a **land** cell.
- If `isWater[i][j] == 1`, cell `(i, j)` is a **water** cell.

You must assign each cell a height in a way that follows these rules:

- The height of each cell must be non-negative.
- If the cell is a **water** cell, its height must be `0`.
- 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).

Find an assignment of heights such that the maximum height in the matrix is **maximized**.

**Example 1:**
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.

**Example 1:**

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: isWater = [[0,1],[0,0]]
Output: [[1,0],[2,1]]
Explanation: The image shows the assigned heights of each cell.
The blue cell is the water cell, and the green cells are the land cells.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.png)

### 思路1
> ...
Map of Highest Peak
```go
```

Input: isWater = [[0,0,1],[1,0,0],[0,0,0]]
Output: [[1,1,0],[0,1,1],[1,2,2]]
Explanation: A height of 2 is the maximum possible height of any assignment.
Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.
```

## 结语

Expand Down
40 changes: 38 additions & 2 deletions leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(isWater [][]int) [][]int {
var dir = [][2]int{
{0, 1}, {0, -1}, {1, 0}, {-1, 0},
}
queue := [][2]int{}
m, n := len(isWater), len(isWater[0])
res := make([][]int, m)
for i := range m {
res[i] = make([]int, n)
for j := range n {
res[i][j] = -1
}
}
for i := range m {
for j := range n {
if isWater[i][j] == 1 {
queue = append(queue, [2]int{i, j})
res[i][j] = 0
}
}
}
h := 1
for len(queue) > 0 {
nq := make([][2]int, 0)
for _, cur := range queue {
for _, d := range dir {
nx, ny := cur[0]+d[0], cur[1]+d[1]
if nx >= 0 && nx < m && ny >= 0 && ny < n && res[nx][ny] == -1 {
res[nx][ny] = h
nq = append(nq, [2]int{nx, ny})
}
}
}
queue = nq
h++
}

return res
}
13 changes: 6 additions & 7 deletions leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{0, 1}, {0, 0}}, [][]int{{1, 0}, {2, 1}}},
{"TestCase2", [][]int{{0, 0, 1}, {1, 0, 0}, {0, 0, 0}}, [][]int{{1, 1, 0}, {0, 1, 1}, {1, 2, 2}}},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading