Skip to content

Commit e7f129f

Browse files
authored
Merge pull request #1072 from 0xff-dev/1034
Add solution and test-cases for problem 1034
2 parents 83c7283 + 54e8307 commit e7f129f

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

leetcode/1001-1100/1034.Coloring-A-Border/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [1034.Coloring A Border][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 `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.
5+
6+
Two squares are called **adjacent** if they are next to each other in any of the 4 directions.
7+
8+
Two squares belong to the same **connected component** if they have the same color and they are adjacent.
9+
10+
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).
11+
12+
You should color the **border** of the **connected component** that contains the square `grid[row][col]` with `color`.
13+
14+
Return the final grid.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
20+
Output: [[3,3],[3,2]]
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Coloring A Border
23-
```go
2425
```
26+
Input: grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3
27+
Output: [[1,3,3],[2,3,3]]
28+
```
29+
30+
**Example 3:**
2531

32+
```
33+
Input: grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2
34+
Output: [[2,2,2],[2,1,2],[2,2,2]]
35+
```
2636

2737
## 结语
2838

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

3-
func Solution(x bool) bool {
4-
return x
3+
var dirs1034 = [][2]int{
4+
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
5+
}
6+
7+
func Solution(grid [][]int, row int, col int, color int) [][]int {
8+
m, n := len(grid), len(grid[0])
9+
initColor := grid[row][col]
10+
11+
queue := [][2]int{{row, col}}
12+
v := map[[2]int]struct{}{
13+
[2]int{row, col}: struct{}{},
14+
}
15+
16+
pos := make([][2]int, 0)
17+
for len(queue) > 0 {
18+
nq := make([][2]int, 0)
19+
for _, i := range queue {
20+
for _, d := range dirs1034 {
21+
nx, ny := i[0]+d[0], i[1]+d[1]
22+
if nx < 0 || nx >= m || ny < 0 || ny >= n || grid[nx][ny] != initColor {
23+
pos = append(pos, i)
24+
continue
25+
}
26+
key := [2]int{nx, ny}
27+
if _, ok := v[key]; !ok {
28+
v[key] = struct{}{}
29+
nq = append(nq, key)
30+
}
31+
}
32+
}
33+
queue = nq
34+
}
35+
for _, p := range pos {
36+
grid[p[0]][p[1]] = color
37+
}
38+
return grid
539
}

leetcode/1001-1100/1034.Coloring-A-Border/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
grid [][]int
14+
row, col, color int
15+
expect [][]int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{1, 1}, {1, 2}}, 0, 0, 3, [][]int{{3, 3}, {3, 2}}},
18+
{"TestCase2", [][]int{{1, 2, 2}, {2, 3, 2}}, 0, 1, 3, [][]int{{1, 3, 3}, {2, 3, 3}}},
19+
{"TestCase3", [][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, 1, 1, 2, [][]int{{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.grid, c.row, c.col, c.color)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
28+
c.expect, got, c.grid, c.row, c.col, c.color)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)