Last Updated: 31 Oct, 2020

Number of Islands II

Moderate
Asked in companies
IBMAmazonChegg Inc.

Problem statement

You are given two integers 'n' and 'm', the dimensions of a grid. The coordinates are from (0, 0) to (n - 1, m - 1).


The grid can only have values 0 and 1.


The value is 0 if there is water and 1 if there is land.


An island is a group of ‘1’s such that every ‘1’ has at least another ‘1’ sharing a common edge.


You are given an array 'queries' of size 'q'.


Each element in 'queries' contains two integers 'x' and 'y', referring to the coordinates in the grid.


Initially, the grid is filled with 0, which means only water and no land.


At each query, the value of 'grid' at (x, y) becomes 1, which means it becomes land.


Find out, after each query, the number of islands in the grid.


Example :
Input: 'n' = 3, 'm' = 4
'queries' = [[1, 1], [1, 2], [2, 3]]

Output: [1, 1, 2]

Explanation:

Initially, the grid was:
0 0 0 0
0 0 0 0
0 0 0 0

After query (1, 1):
0 0 0 0
0 1 0 0
0 0 0 0
There is one island having land (1, 1).

After query (1, 2):
0 0 0 0
0 1 1 0
0 0 0 0
Since (1, 1) and (1, 2) share a common edge, they form one island. So there is one island having lands (1, 1) and (1, 2).

After query (2, 3):
0 0 0 0
0 1 1 0
0 0 0 1
Now there are two islands, one having lands (1, 1) and (1, 2), and another having land (2, 3).

So the number of islands after each query is [1, 1, 2].
Input Format :
The first line contains two integers, 'n' and 'm' denoting the number of rows and columns in the grid, respectively.

The second line contains an integer 'q' denoting the number of queries.

The next 'q' lines contain two integers each, the coordinates which are becoming land.


Output Format :
Print an array of size 'q', denoting the answer of each query.


Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.

Approaches

01 Approach

We can use Disjoint Set Union to keep track of connected lands efficiently.

We can make a DSU of size 'n' * 'm', and for every coordinate (i, j), we can map them to 'i' * 'm' + 'j' in the DSU.

At any query (x, y), we will check all four directions. If there is land in the adjacent coordinate, we will merge them and manage the total number of islands.

The steps are as follows:

int[] numberOfIslandsII(int 'n', int 'm', int 'queries[][]', int 'q')

  1. Initialise a DSU of size 'n' * 'm'.
  2. Let 'ans' be an array of size 'q'.
  3. Let 'count' = 0 be the variable to store the number of islands after each query.
  4. Let 'grid' be a 2-D matrix of size 'n' * 'm' and filled with 0. This is used to store the coordinates converted into land.
  5. For each 'query' in 'queries':
    • Let 'x' = query[0], 'y' = query[1]
    • 'grid[x][y]' = 1
    • First, we will consider (x, y) as an island. So increase 'count' by 1.
    • Now, for each of the four directions, do the following:
      • If the coordinate at the respective direction of (x, y) exists and is equal to 1, this means (x, y) can be merged to that coordinate’s island group. So:
        • Merge (x, y) and that coordinate.
        • Decrease count by 1.
    • Append 'count' in 'ans'.
  6. Return ‘ans’.