Number of Islands II

Hard
0/120
Average time to solve is 45m
profile
Contributed by
59 upvotes
Asked in companies
UberFlipkartMakeMyTrip

Problem statement

You have a 2D grid of ‘N’ rows and ‘M’ columns which are initially filled with water. You are given ‘Q’ queries each consisting of two integers ‘X’ and ‘Y’ and in each query operation, you have to turn the water at position (‘X’, ‘Y’) into a land. You are supposed to find the number of islands in the grid after each query.

An island is a group of lands surrounded by water horizontally, vertically, or diagonally.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer ‘T’ denoting the number of test cases. 

The first input line of each test case contains two single space-separated integers ‘N’ and ‘M’ representing the number of rows and columns of the grid, respectively.

The second line of each test case contains an integer ‘Q’ representing the number of queries.

Next ‘Q’ lines contain two single space-separated integers ‘X’ and ‘Y’, representing the coordinates of the grid i.e the coordinates of the point to be turned into land.
Output Format:
For each test case, print a single integer denoting the number of islands after each query operation.

Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 1000
1 <= M <= 1000
1 <= Q <= 100000
0 <= X < N
0 <= Y < M

Time limit: 1 sec
Sample Input 1:
2
3 3
4
0 0
0 1
1 2
2 1
4 5
4
1 1
0 1
3 3
3 4
Sample Output 1:
1 1 2 3
1 1 2 2
Explanation of Sample Output 1:
In test case 1, 

0.  000
    000
    000

1.  100
    000
    000

2.  110
    000
    000

 3. 110
    001
    000

 4. 110
    001
    100

So, the answer is 1, 1, 2, 3.

In test case 2,

0.  00000
    00000
    00000
    00000

1.  00000
    01000
    00000
    00000

2.  01000
    01000
    00000
    00000

 3. 01000
    01000
    00000
    00010

 4. 01000
    01000
    00000
    00011

So, the answer is 1, 1, 2, 2.
Sample Input 2:
2
2 2
2
0 0
1 1
1 1
1
0 0
Sample Output 2:
1 2
1
Explanation of Sample Output 2:
In test case 1, 

0.  00
    00

1.  10
    00

2.  10
    01

So, the answer is 1, 2.

In test case 2,

0.  0

1.  1

So, the answer is 1.
Hint

Can you think of some brute force approach to find the islands connected to each other?

Approaches (2)
Brute Force Approach

The idea here is to represent the grid as a graph and all the adjacent land cells are connected via an edge. Finally, do DFS on the grid and find the number of connected components after each query operation.

 

The algorithm is as follows:

  1. Declare an ‘ANS’ array to store the number of islands after each query.
  2. Declare a 2D array 'GRID' with ‘N’ rows and ‘M’ columns, where ‘GRID[i][j]’ = 0 or 1, 0 representing water and 1 representing land.
  3. Declare a ‘DX’ array representing and set it to {1, 0, -1, 0}, representing the directions of all four neighboring cells.
  4. Declare a ‘DY’ array representing and set it to {0, 1, 0, -1}, representing the directions of all four neighboring cells.
  5. Iterate from ‘P’ = 0 to ‘Q.size() - 1’,
    • Declare a variable ‘X’ and set it to ‘Q[i][0]’.
    • Declare a variable ‘Y’ and set it to 'Q[i][1]'.
    • Set ‘GRID[X][Y]’ to 1, i.e i.e cell is converted into the land.
    • Declare a variable 'NUMBEROFISLANDS' and set it to 0.
    • Declare a 2D array ‘VISITED’ with ‘N’ rows and ‘M’ columns, where ‘VISITED[i][j]’ = 0 or 1, 0 representing the current cell being visited and 1 representing not visited.
    • Iterate from ‘i’ = 0 to ‘N’ - 1,
      • Iterate from ‘j’ = 0 toM’ - 1,
        • If ‘GRID[i][j]’ == 1 && ‘VISITED[i][j]’ == 0,
          • This means the current cell is an island that is not seen before.
          • Run a ‘DFS’ function and mark all the connected lands as ‘VISITED’ that are connected to the cell (i,j).
          • Increment 'NUMBEROFISLANDS' by 1.
    • Append 'NUMBEROFISLANDS' to ‘ANS’.
  6. Return the ‘ANS’ array.
Time Complexity

O(Q * N * M), Where ‘Q’ is the number of queries, ‘N’ is the number of rows in the given grid, and ‘M’ is the number of columns. 

 

Since for each query run a DFS on the grid to count the number of connected components which is of order N * M, So, the overall time complexity is O(Q * N * M).

Space Complexity

O(N * M), Where ‘N’ is the number of rows in the given grid, and ‘M’ is the number of columns. 

 

Since we are using two 2D matrices which are of order N * M. So, the overall space complexity is O(N * M).

Code Solution
(100% EXP penalty)
Number of Islands II
Full screen
Console