diff --git a/leetcode/1701-1800/1765.Map-of-Highest-Peak/1.png b/leetcode/1701-1800/1765.Map-of-Highest-Peak/1.png new file mode 100644 index 000000000..4402cd710 Binary files /dev/null and b/leetcode/1701-1800/1765.Map-of-Highest-Peak/1.png differ diff --git a/leetcode/1701-1800/1765.Map-of-Highest-Peak/2.png b/leetcode/1701-1800/1765.Map-of-Highest-Peak/2.png new file mode 100644 index 000000000..215f2c0f9 Binary files /dev/null and b/leetcode/1701-1800/1765.Map-of-Highest-Peak/2.png differ diff --git a/leetcode/1701-1800/1765.Map-of-Highest-Peak/README.md b/leetcode/1701-1800/1765.Map-of-Highest-Peak/README.md index c7e656ff1..71019e9a0 100755 --- a/leetcode/1701-1800/1765.Map-of-Highest-Peak/README.md +++ b/leetcode/1701-1800/1765.Map-of-Highest-Peak/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution.go b/leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution.go index d115ccf5e..b2ddd79a9 100644 --- a/leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution.go +++ b/leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution.go @@ -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 } diff --git a/leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution_test.go b/leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution_test.go index 14ff50eb4..9e6841291 100644 --- a/leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution_test.go +++ b/leetcode/1701-1800/1765.Map-of-Highest-Peak/Solution_test.go @@ -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}}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }