LeetCode 827. Making A Large Island
原题链接在这里:https://leetcode.com/problems/making-a-large-island/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: 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:
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.
Constraints:
n == grid.length
n == grid[i].length
1 <= n <= 500
grid[i][j]
is either0
or1
.
题解:
Do two rounds of traversal.
In the first round, when encountering 1, paint the connect island to a unique index, also calculate the area of this connected island. Put index -> area into map.
In the seond round, when encounting 0, check all 4 sourding neighbors and combine them togerther and update the maximum res.
Note: 1. in the paint, stop condition is grid[i][j] != 1, but not grid[i][j] == 0. This is because when mark grid[i][j] = index, like 2. In the next layer of dfs, it came back to this position and it is not equal to 0, will not stop and get into infinite loop.
2. In the second traversal, the some neighbors could be connected already. Thus need a visited set to mark the added index.
Time Complexity: O(m * n). m = grid.length. n = grid[0].length.
Space: O(m * n).
AC Java:
1 class Solution { 2 int [][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; 3 public int largestIsland(int[][] grid) { 4 if(grid == null || grid.length == 0 || grid[0].length == 0){ 5 return 0; 6 } 7 8 int m = grid.length; 9 int n = grid[0].length; 10 HashMap<Integer, Integer> hm = new HashMap<>(); 11 int index = 2; 12 int res = 0; 13 for(int i = 0; i < m; i++){ 14 for(int j = 0; j < n; j++){ 15 if(grid[i][j] == 1){ 16 int area = paint(grid, i, j, index); 17 hm.put(index, area); 18 index++; 19 res = Math.max(res, area); 20 } 21 } 22 } 23 24 25 for(int i = 0; i < m; i++){ 26 for(int j = 0; j < n; j++){ 27 if(grid[i][j] == 0){ 28 HashSet<Integer> visited = new HashSet<>(); 29 int combine = 1; 30 for(int [] dir : dirs){ 31 int x = i + dir[0]; 32 int y = j + dir[1]; 33 if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0 || visited.contains(grid[x][y])){ 34 continue; 35 } 36 visited.add(grid[x][y]); 37 combine += hm.get(grid[x][y]); 38 } 39 40 res = Math.max(res, combine); 41 } 42 } 43 } 44 45 return res; 46 } 47 48 private int paint(int[][] grid, int i, int j, int index){ 49 if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1){ 50 return 0; 51 } 52 53 grid[i][j] = index; 54 55 int res1 = paint(grid, i + 1, j, index); 56 int res2 = paint(grid, i - 1, j, index); 57 int res3 = paint(grid, i, j + 1, index); 58 int res4 = paint(grid, i, j - 1, index); 59 return 1 + res1 + res2 + res3 + res4; 60 } 61 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园2025新款「AI繁忙」系列T恤上架,前往周边小店选购
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步