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
33 changes: 20 additions & 13 deletions leetcode/801-900/0827.Making-A-Large-Island/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [827.Making A Large Island][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 `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.

Return the size of the largest **island** in `grid` after applying this operation.

An **island** is a 4-directionally connected group of `1`s.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: grid = [[1,0],[0,1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
```

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

### 思路1
> ...
Making A Large Island
```go
```
Input: grid = [[1,1],[1,0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
```

**Example 3:**

```
Input: grid = [[1,1],[1,1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 4.
```

## 结语

Expand Down
76 changes: 74 additions & 2 deletions leetcode/801-900/0827.Making-A-Large-Island/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,77 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(grid [][]int) int {
group := 1
groupMapCount := map[int]int{}
rows, cols := len(grid), len(grid[0])
groupGrid := make([][]int, rows)
for i := range rows {
groupGrid[i] = make([]int, cols)
}

var (
bfs func(int, int) int
dirs = [][2]int{
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
}
)

bfs = func(x, y int) int {
q := [][2]int{{x, y}}
cnt := 0
groupGrid[x][y] = group
for len(q) > 0 {
cnt += len(q)
nq := make([][2]int, 0)
for _, cur := range q {
for _, d := range dirs {
nx, ny := cur[0]+d[0], cur[1]+d[1]
if nx < 0 || nx >= rows || ny < 0 || ny >= cols {
continue
}
if grid[nx][ny] == 1 && groupGrid[nx][ny] == 0 {
groupGrid[nx][ny] = group
nq = append(nq, [2]int{nx, ny})
}
}
}
q = nq
}
return cnt
}

ans := 0
for i := range rows {
for j := range cols {
if grid[i][j] == 1 && groupGrid[i][j] == 0 {
groupMapCount[group] = bfs(i, j)
ans = max(ans, groupMapCount[group])
group++
}
}
}

for i := range rows {
for j := range cols {
if grid[i][j] == 0 {
tmp := 1
used := map[int]struct{}{}
for _, d := range dirs {
nx, ny := i+d[0], j+d[1]
if nx < 0 || nx >= rows || ny < 0 || ny >= cols {
continue
}
if groupGrid[nx][ny] != 0 {
if _, ok := used[groupGrid[nx][ny]]; !ok {
tmp += groupMapCount[groupGrid[nx][ny]]
used[groupGrid[nx][ny]] = struct{}{}
}
}
}
ans = max(ans, tmp)
}
}
}

return ans
}
14 changes: 7 additions & 7 deletions leetcode/801-900/0827.Making-A-Large-Island/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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{{1, 0}, {0, 1}}, 3},
{"TestCase2", [][]int{{1, 1}, {1, 0}}, 4},
{"TestCase3", [][]int{{1, 1}, {1, 1}}, 4},
}

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

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

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