The original title link is here: https://leetcode.com/problems/number-of-islands-ii/
A 2d grid Map of m
rows and n
columns is initially filled with water. We may perform an addland operation which turns the water at position (row, col) to a land. Given a list of positions to operate, count the number of the islands in each Addland operation. An island is surrounded by water and are formed by connecting adjacent lands horizontally or vertically. Assume all four edges of the grid is all surrounded by water.
Example:
Given m = 3, n = 3
, positions = [[0,0], [0,1], [1,2], [2,1]]
.
Initially, the 2d grid is grid
filled with water. (Assume 0 represents water and 1 represents land).
0 0 00 0 00 0 0
Operation #1: Addland (0, 0) turns the water at Grid[0][0] to a land.
1 0 XX 0 0 number of Islands = 10 0 0
Operation #2: Addland (0, 1) turns the water at Grid[0][1] to a land.
1 1 XX 0 0 Number of Islands = 10 0 0
Operation #3: Addland (1, 2) turns the water at Grid[1][2] to a land.
1 1 XX 0 1 number of Islands = 20 0 0
Operation #4: Addland (2, 1) turns the water at Grid[2][1] to a land.
1 1 XX 0 1 number of Islands = 30 1 0
We return the result as an array:[1, 1, 2, 3]
Challenge:
Can do it in time complexity O (K-Log mn), where k is the length of the positions
?
Union-find topic. is the number of islands upgrade.
A operation is added into the unionfind2d type of islands. Look at the four direction of the islands in the parent corresponding to the value, if greater than 0, see if it is in a heel, if not, the union up, at the same time. The four direction go through and add the current count of islands to the Res.
Time Complexity:o (K*LOGMN). Space:o (MN).
AC Java:
1 Public classSolution {2 int[] Directions = {{0,1},{0,-1},{-1,0},{1,0}};3 PublicList<integer> NumIslands2 (intMintNint[] positions) {4list<integer> res =NewArraylist<integer>();5 if(m = = 0 | | n = = 0 | | positions = =NULL|| Positions.length = = 0){6 returnRes;7 }8 9unionfind2d Islands =Newunionfind2d (M, n);Ten for(int[] row:positions) { One intp = Islands.add (row[0],row[1]); A for(int[] dir:directions) { - intx = row[0] + dir[0]; - inty = row[1] + dir[1]; the intQ =islands.getparent (x, y); - if(q > 0 &&!)Islands.find (P,Q)) { - islands.union (p,q); - } + } - Res.add (Islands.size ()); + } A returnRes; at } - } - - classunionfind2d{ - Private int[] parent; - Private int[] size; in Private intcount, M, N; - to PublicUNIONFIND2D (intMintN) { + This. m =m; - This. N =N; the This. Count = 0; *Parent =New int[M*n+1]; $Size =New int[M*n+1];Panax Notoginseng } - the Private intGetIndex (intIintj) { + returnI*n + j + 1; A } the + Public intAddintIintj) { - intindex =GetIndex (i,j); $count++; $Parent[index] =index; -Size[index] = 1; - returnindex; the } - Wuyi Public intGetParent (intIintj) { the if(I < 0 | | i>=m | | j<0 | | j>=N) { - return0; Wu } - returnParent[getindex (i,j)]; About } $ - Public intsize () { - return This. Count; - } A + Public BooleanFindintPintq) { the returnRoot (p) = =root (q); - } $ the Private intRootinti) { the if(Parent[i] = = 0){ the returni; the } - while(I! =Parent[i]) { inParent[i] =Parent[parent[i]]; thei =Parent[i]; the } About returni; the } the the Public voidUnionintPintq) { + inti =root (p); - intj =root (q); the if(Size[i] <Size[j]) {BayiParent[i] =J; theSIZE[J] + =Size[i]; the}Else{ -PARENT[J] =i; -Size[i] + =Size[j]; the } thecount--; the } the}
Leetcode Number of Islands II