Skip to content

Commit b661102

Browse files
committed
Add solution and test-cases for problem 827
1 parent d569e0c commit b661102

File tree

3 files changed

+101
-22
lines changed

3 files changed

+101
-22
lines changed

leetcode/801-900/0827.Making-A-Large-Island/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [827.Making A Large Island][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 `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
5+
6+
Return the size of the largest **island** in `grid` after applying this operation.
7+
8+
An **island** is a 4-directionally connected group of `1`s.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: grid = [[1,0],[0,1]]
14+
Output: 3
15+
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Making A Large Island
23-
```go
2420
```
21+
Input: grid = [[1,1],[1,0]]
22+
Output: 4
23+
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
24+
```
25+
26+
**Example 3:**
2527

28+
```
29+
Input: grid = [[1,1],[1,1]]
30+
Output: 4
31+
Explanation: Can't change any 0 to 1, only one island with area = 4.
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) int {
4+
group := 1
5+
groupMapCount := map[int]int{}
6+
rows, cols := len(grid), len(grid[0])
7+
groupGrid := make([][]int, rows)
8+
for i := range rows {
9+
groupGrid[i] = make([]int, cols)
10+
}
11+
12+
var (
13+
bfs func(int, int) int
14+
dirs = [][2]int{
15+
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
16+
}
17+
)
18+
19+
bfs = func(x, y int) int {
20+
q := [][2]int{{x, y}}
21+
cnt := 0
22+
groupGrid[x][y] = group
23+
for len(q) > 0 {
24+
cnt += len(q)
25+
nq := make([][2]int, 0)
26+
for _, cur := range q {
27+
for _, d := range dirs {
28+
nx, ny := cur[0]+d[0], cur[1]+d[1]
29+
if nx < 0 || nx >= rows || ny < 0 || ny >= cols {
30+
continue
31+
}
32+
if grid[nx][ny] == 1 && groupGrid[nx][ny] == 0 {
33+
groupGrid[nx][ny] = group
34+
nq = append(nq, [2]int{nx, ny})
35+
}
36+
}
37+
}
38+
q = nq
39+
}
40+
return cnt
41+
}
42+
43+
ans := 0
44+
for i := range rows {
45+
for j := range cols {
46+
if grid[i][j] == 1 && groupGrid[i][j] == 0 {
47+
groupMapCount[group] = bfs(i, j)
48+
ans = max(ans, groupMapCount[group])
49+
group++
50+
}
51+
}
52+
}
53+
54+
for i := range rows {
55+
for j := range cols {
56+
if grid[i][j] == 0 {
57+
tmp := 1
58+
used := map[int]struct{}{}
59+
for _, d := range dirs {
60+
nx, ny := i+d[0], j+d[1]
61+
if nx < 0 || nx >= rows || ny < 0 || ny >= cols {
62+
continue
63+
}
64+
if groupGrid[nx][ny] != 0 {
65+
if _, ok := used[groupGrid[nx][ny]]; !ok {
66+
tmp += groupMapCount[groupGrid[nx][ny]]
67+
used[groupGrid[nx][ny]] = struct{}{}
68+
}
69+
}
70+
}
71+
ans = max(ans, tmp)
72+
}
73+
}
74+
}
75+
76+
return ans
577
}

leetcode/801-900/0827.Making-A-Large-Island/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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{{1, 0}, {0, 1}}, 3},
17+
{"TestCase2", [][]int{{1, 1}, {1, 0}}, 4},
18+
{"TestCase3", [][]int{{1, 1}, {1, 1}}, 4},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)