Problem of the day
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.
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].
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.
Print an array of size 'q', denoting the answer of each query.
You do not need to print anything; it has already been taken care of. Just implement the given function.
3 4
3
1 1
1 2
2 3
1 1 2
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].
3 5
3
1 1
1 3
1 2
1 2 1
The expected time complexity is O(n * m + q).
1 <= 'n', 'm' <= 1000
1 <= 'q' <= 10 ^ 4
0 <= 'x' < 'n'
0 <= 'y' < 'm'
All the (x, y) pairs are unique.
Time limit: 1 second
We can use the Disjoint Set Union data structure to track connected lands efficiently.
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')
O(n * m + q), Where 'n' and 'm' are the dimensions of the grid, and 'q' is the number of queries.
The initialisation of the DSU data structure is linear time, and we have DSU of size 'n' * 'm'.
The queries loop will run at most 4 * 'q' times.
Hence the time complexity is O(n * m + q).
O(n * m + q), Where 'n' and 'm' are the dimensions of the grid, and 'q' is the number of queries.
The DSU data structure and 'grid' matrix consume space O(n * m).
The 'ans' array is of size O(q).
Hence the space complexity is O(n * m + q).