Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of DFS and Disjoint Set Union but will also help you build up problem-solving skills.
👁 Image
Given a binary matrix of size n x n, the task is to find the largest island after changing at most one cell from 0 to 1.
Note: An island is a 4-directionally (i.e, up, down, right, left) connected group of 1s.
Example 1:
👁 Image
Example 2:
👁 Image
We recommend you to try this problem on our GeeksforGeeks Practice portal first, and maintain your streak to earn Geeksbits and other exciting prizes, before moving towards the solution.
The idea is to keep track of each island by some unique identity say key/name with its size. After this, go through each cell which is water (i.e, matrix[i][j] == 0) and add the sizes of unique neighbour’s island into current size, finally maximize the current size in the result.
Follow the steps below to implement the above idea:
- Initialize a result variable for storing the largest island.
- Create an unordered map to store identity of each island with its sizes.
- Explore each island and update the cell’s value with a unique key and finally store key and size of island in map.
- Iterate through each cell of the grid. which represents water or 0’s:
- Get sizes of neighboring islands from the map.
- Calculate combined size of neighboring islands and the current cell.
- Update the result with the maximum calculated size.
Below is the implementation of the above approach:
Time Complexity: O(n2)
- The two nested loops iterate through all cells in the grid, resulting in O(n2) iterations.
- Inside the loops, constant time operations are performed for DFS exploration and island size calculations.
- The overall time complexity is O(n2), where n is the size of the grid.
Auxiliary Space: O(n2)
- The visited matrix requires O(n2) space.
- The unordered map stores island sizes, potentially up to O(n2) distinct islands.
- Other variables and data structures used occupy constant space.
- The overall space complexity is O(n2).
The idea is to use disjoint set union to find our answer we can do this by first making add the islands as the union and storing the size of each island and then we can find the sum of the size of adjacent island for all the values in the grid which are 0.
- Initialize a disjoint class ds and two vector dr and dc which are used to move the four adjacent cells for any cell.
- Traverse through the grid,
- If we get the grid cell with value 1, then we move to its four adjacent cells and make the Union by using the unionBySize function of current cell and the adjacent cells.
- Traverse through the grid,
- If we get a cell with the value 0 then we see all of its four adjacent neighbours, if any of its neighbours is 1 then for each we insert the the parent of that adjacent node into the set using the findUPar function.
- Intialize a variable sizeTotal equal to 0 and then we iterate the set and add the size of the parent cells into the sizeTotal.
- Update the mx with maximum of mx and sizeTotal+1, we add 1 as the current cell is also included.
- But if the matrix contains only 1 then to consider that we traverse for every node from 0 to n*n and find the size of its parent and then update mx with maximum of mx and size of the parent.
- Return mx.
Time Complexity: O(N2), As we are traversing the grid of size N*N for two times and other operation will take constant time, So our complexity becomes O(N*N).
Auxiliary Space: O(N2), As we are making three arrays of size N*N.