diff --git a/leetcode/801-900/0827.Making-A-Large-Island/README.md b/leetcode/801-900/0827.Making-A-Large-Island/README.md index c762da5d1..9f7576543 100644 --- a/leetcode/801-900/0827.Making-A-Large-Island/README.md +++ b/leetcode/801-900/0827.Making-A-Large-Island/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/801-900/0827.Making-A-Large-Island/Solution.go b/leetcode/801-900/0827.Making-A-Large-Island/Solution.go index d115ccf5e..ddcec3b12 100644 --- a/leetcode/801-900/0827.Making-A-Large-Island/Solution.go +++ b/leetcode/801-900/0827.Making-A-Large-Island/Solution.go @@ -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 } diff --git a/leetcode/801-900/0827.Making-A-Large-Island/Solution_test.go b/leetcode/801-900/0827.Making-A-Large-Island/Solution_test.go index 14ff50eb4..f963a6ad3 100644 --- a/leetcode/801-900/0827.Making-A-Large-Island/Solution_test.go +++ b/leetcode/801-900/0827.Making-A-Large-Island/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }