An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.
You are also given three integers sr, sc, and newColor. You should perform a flood fill on the image starting from the pixel image[sr][sc].
To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with newColor.
Return the modified image after performing the flood fill.
Example 1:
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with a value 1 in the island.
Return the maximum area of an island in grid. If there is no island, return 0.
Example 1:
Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output: 6
Explanation: The answer is not 11, because the island must be connected 4-directionally.
Example 2:
Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 50
grid[i][j] is either 0 or 1.
Solution
Approach #0: BFS
var ( dx = []int{0, 1, 0, -1} dy = []int{1, 0, -1, 0})funcmaxAreaOfIsland(grid [][]int) int {iflen(grid) ==0 {return0 } m, n :=len(grid), len(grid[0]) res :=0for i :=0; i < m; i++ {for j :=0; j < n; j++ {if grid[i][j] ==0 {continue } grid[i][j] =0 queue := [][]int{{i, j}} area :=1for ii :=0; ii <len(queue); ii++ { cell := queue[ii]for jj :=0; jj <4; jj++ { x, y := cell[0]+dx[jj], cell[1]+dy[jj]if x >=0&& x < m && y >=0&& y < n && grid[x][y] >0 { grid[x][y] =0 area++ queue =append(queue, []int{x, y}) } } } res =max(res, area) } }return res}funcmax(a, b int) int {if a > b {return a }return b}
Approach #1: DFS (Recursive)
var ( dx = []int{0, 1, 0, -1} dy = []int{1, 0, -1, 0})funcmaxAreaOfIsland(grid [][]int) int {iflen(grid) ==0 {return0 } res :=0for i :=0; i <len(grid); i++ {for j :=0; j <len(grid[0]); j++ {if grid[i][j] ==0 {continue } area :=dfs(grid, i, j) res =max(res, area) } }return res}funcdfs(grid [][]int, x, y int) int { grid[x][y] =0 res :=1for i :=0; i <4; i++ { xx, yy := x+dx[i], y+dy[i]if xx >=0&& xx <len(grid) && yy >=0&& yy <len(grid[0]) && grid[xx][yy] >0 { res = res +dfs(grid, xx, yy) } }return res}funcmax(a, b int) int {if a > b {return a }return b}
Approach #2: DFS + Stack (Iterative)
var ( dx = []int{0, 1, 0, -1} dy = []int{1, 0, -1, 0})funcmaxAreaOfIsland(grid [][]int) int {iflen(grid) ==0 {return0 } res :=0for i :=0; i <len(grid); i++ {for j :=0; j <len(grid[0]); j++ { s := [][]int{{i, j}} area :=0forlen(s) >0 { cell := s[len(s)-1] s = s[:len(s)-1] x, y := cell[0], cell[1]if x <0|| x >=len(grid) || y <0|| y >=len(grid[0]) || grid[x][y] ==0 {continue } grid[x][y] =0 area++for i :=0; i <4; i++ { xx, yy := x+dx[i], y+dy[i] s =append(s, []int{xx, yy}) } } res =max(res, area) } }return res}funcmax(a, b int) int {if a > b {return a }return b}