COUNT ISLANDS

Moderate
0/80
Average time to solve is 40m
profile
Contributed by
0 upvote

Problem statement

You are given an empty 2D binary 'GRID' of size ‘N’ x 'M' The 'GRID' represents a map where 0's represent water and 1's represent land. Initially, all the cells of the 'GRID' are water cells (i.e., all the cells are 0's).

We may perform an add land operation which turns the water at position into a land. You are given an array 'POSITIONS' of size ‘K’ where POSITIONS[i] = [Ri, Ci] is the position (Ri, Ci) at which we should operate the ith operation.

Return an array of integers answer where ‘ANSWER[i]’ is the number of islands after turning the cell (‘Ri’, ‘Ci’) into a land.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the 'GRID' are all surrounded by water.

Example:
Input: ‘M’ = 3, 'N' = 3, 'POSITIONS' = [ [ 0,0 ], [ 0,1 ], [ 1,2 ], [ 2,1 ] ]

Output: [1, 1, 2, 3]

Initially, the 2d  'GRID' is filled with water.
- Operation #1: addLand(0, 0) turns the water at  ‘GRID[0][0]’ into a land. We have 1 island.
- Operation #2: addLand(0, 1) turns the water at  ‘GRID[0][1]’ into a land. We still have 1 island.
- Operation #3: addLand(1, 2) turns the water at  'GRID[1][2]’ into a land. We have 2 islands.
- Operation #4: addLand(2, 1) turns the water at  'GRID[2][1]’ into a land. We have 3 islands.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains ‘T’, denoting the number of test cases.    

The first line of each test case contains two integers, ‘N’ and ‘M’, denoting the number of rows and columns in the  'GRID'.

The second line of each test case contains an integer ‘K’ denoting the size of ‘POSITION’ array.

Each of the next ‘K’ lines contains two space-separated integers denoting the (‘Ri’, ‘Ci’) of ‘POSITION[i]’.
Output format :
For each test case, return the array of integers as described in the problem statement.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= M, N, K <= 100
0 <= Ri< m
0 <= Ci < n

Time Limit: 1 sec
Sample Input 1 :
2
1 3
3
0 0
0 1
0 2
1 3
3
0 0
0 2
0 1

##### Sample Output 1 :

 1 1 1 
 1 2 1 
Explanation Of Sample Input 1 :
For the first test case:-
- Operation #1: addLand(0, 0) turns the water at  ‘GRID’[0][0] into a land. We have 1 island,  [( 0, 0)].
- Operation #2: addLand(0, 1) turns the water at  ‘GRID’[0][1] into a land. We still have 1     island,  [ ( 0, 0) , ( 0, 1 ) ].
- Operation #3: addLand(0, 2) turns the water at  'GRID'[1][2] into a land. We still have 1 island,  [ ( 0, 0) , ( 0, 1 ), ( 0, 2 ) ].


For the second test case:-
   - Operation #1: addLand(0, 0) turns the water at  ‘GRID’[0][0] into a land. We have 1 island, [ ( 0, 0 ) ].
- Operation #2: addLand(0, 2) turns the water at  ‘GRID’[0][1] into a land. We still have 2    islands, [ ( 0, 0 ) ] and [ ( 0, 2 ) ].
- Operation #3: addLand(0, 2) turns the water at  'GRID'[1][2] into a land. We have 1 island,  [ ( 0, 0) , ( 0, 1 ), ( 0, 2 ) ].
Sample Input 2 :
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 2 :
 1 1 2 3 
 1 1 2 2 
Hint

you can apply for DFS in the  'GRID' for every element ‘POSITION’.

Approaches (2)
Brute Force

Linear scan the 2d grid map, if a node contains a '1', then it is a root node that triggers a Depth First Search. During DFS, every visited node should be set as '0' to mark it as a visited node. Count the number of root nodes that trigger DFS, this number would be the number of islands since each DFS starting at some root identifies an island.


 

The steps are as follows:


 

  1. In the function ‘numIslands’, for each cell in ‘POSITIONS’ array, starting from the beginning, we set the value of the corresponding cell as ‘1’ and call the ‘numIslandsUtil’ function passing the current configuration of ‘GRID’.
  2. We initially have an empty array ‘numIslandsArr’ in which we append the values returned by ‘numIslandsUtil’ function and return it at the last.
  3. In the function numIslandsUtil, we scan the 2-D ‘GRID’ from top to bottom if we find a cell with value ‘1’ then we can call the dfs function and pass the cell as one of it’s parameters.
  4. In dfs function, we set the value of current cell as ‘0’ then, we iterate through it’s 4-directional neighbours.
  5. During iteration, if any of the neighbour cell is found as ‘1’ then, we call the dfs function on it.
  6. We have a counter variable in numIslandsUtil function, which we increment every time we call dfs function and return it at the last.
Time Complexity

O(N * M *K ), where 'N'  is the number of rows in the given  'GRID',  'M' is the number of columns and ‘K’ is the size of the ‘POSITIONS’ array.

 As we have at most N * M operations for every element in 'POSITIONS' of size K.

Hence, the time complexity is O ( M * N *K ).

Space Complexity

O( N * M ), where 'N' and 'M' are the dimensions of the  'GRID' .

 

We  need space for the recursion stack.

 

Hence, the space complexity is O ( M * N ).

Code Solution
(100% EXP penalty)
COUNT ISLANDS
Full screen
Console