From 54e83071f9280f0dc921de0e19a1209cd9e34f1d Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 23 Dec 2024 09:33:19 +0800 Subject: [PATCH] Add solution and test-cases for problem 1034 --- .../1034.Coloring-A-Border/README.md | 36 +++++++++++------- .../1034.Coloring-A-Border/Solution.go | 38 ++++++++++++++++++- .../1034.Coloring-A-Border/Solution_test.go | 23 +++++------ 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/leetcode/1001-1100/1034.Coloring-A-Border/README.md b/leetcode/1001-1100/1034.Coloring-A-Border/README.md index a0770ebbd..d20683d6a 100644 --- a/leetcode/1001-1100/1034.Coloring-A-Border/README.md +++ b/leetcode/1001-1100/1034.Coloring-A-Border/README.md @@ -1,28 +1,38 @@ # [1034.Coloring A Border][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 `m x n` integer matrix `grid`, and three integers `row`, `col`, and `color`. Each value in the grid represents the color of the grid square at that location. + +Two squares are called **adjacent** if they are next to each other in any of the 4 directions. + +Two squares belong to the same **connected component** if they have the same color and they are adjacent. + +The **border of a connected component** is all the squares in the connected component that are either adjacent to (at least) a square not in the component, or on the boundary of the grid (the first or last row or column). + +You should color the **border** of the **connected component** that contains the square `grid[row][col]` with `color`. + +Return the final grid. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3 +Output: [[3,3],[3,2]] ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Coloring A Border -```go ``` +Input: grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3 +Output: [[1,3,3],[2,3,3]] +``` + +**Example 3:** +``` +Input: grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2 +Output: [[2,2,2],[2,1,2],[2,2,2]] +``` ## 结语 diff --git a/leetcode/1001-1100/1034.Coloring-A-Border/Solution.go b/leetcode/1001-1100/1034.Coloring-A-Border/Solution.go index d115ccf5e..ac6ccd405 100644 --- a/leetcode/1001-1100/1034.Coloring-A-Border/Solution.go +++ b/leetcode/1001-1100/1034.Coloring-A-Border/Solution.go @@ -1,5 +1,39 @@ package Solution -func Solution(x bool) bool { - return x +var dirs1034 = [][2]int{ + {1, 0}, {-1, 0}, {0, 1}, {0, -1}, +} + +func Solution(grid [][]int, row int, col int, color int) [][]int { + m, n := len(grid), len(grid[0]) + initColor := grid[row][col] + + queue := [][2]int{{row, col}} + v := map[[2]int]struct{}{ + [2]int{row, col}: struct{}{}, + } + + pos := make([][2]int, 0) + for len(queue) > 0 { + nq := make([][2]int, 0) + for _, i := range queue { + for _, d := range dirs1034 { + nx, ny := i[0]+d[0], i[1]+d[1] + if nx < 0 || nx >= m || ny < 0 || ny >= n || grid[nx][ny] != initColor { + pos = append(pos, i) + continue + } + key := [2]int{nx, ny} + if _, ok := v[key]; !ok { + v[key] = struct{}{} + nq = append(nq, key) + } + } + } + queue = nq + } + for _, p := range pos { + grid[p[0]][p[1]] = color + } + return grid } diff --git a/leetcode/1001-1100/1034.Coloring-A-Border/Solution_test.go b/leetcode/1001-1100/1034.Coloring-A-Border/Solution_test.go index 14ff50eb4..23f6877c6 100644 --- a/leetcode/1001-1100/1034.Coloring-A-Border/Solution_test.go +++ b/leetcode/1001-1100/1034.Coloring-A-Border/Solution_test.go @@ -9,31 +9,32 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + grid [][]int + row, col, color int + expect [][]int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{1, 1}, {1, 2}}, 0, 0, 3, [][]int{{3, 3}, {3, 2}}}, + {"TestCase2", [][]int{{1, 2, 2}, {2, 3, 2}}, 0, 1, 3, [][]int{{1, 3, 3}, {2, 3, 3}}}, + {"TestCase3", [][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, 1, 1, 2, [][]int{{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.grid, c.row, c.col, c.color) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v", + c.expect, got, c.grid, c.row, c.col, c.color) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }