Making a Large Island
Master this topic with zero to advance depth.
Making a Large Island
Change at most one 0 to 1 in a binary grid to get the largest possible island.
Examples
Input: [[1,0],[0,1]]
Output: 3
Approach 1
Level III: Union-Find (Component Sizes)
Intuition
Pre-calculate all island sizes using DSU. Then, for each '0', calculate the potential island size by summing sizes of all unique neighbor islands + 1.
⏱ O(N^2 \cdot \alpha(N^2))💾 O(N^2)
Detailed Dry Run
Grid: [[1,0],[0,1]]. DSU islands: {0}:sz=1, {3}:sz=1. Flip (0,1) at index 1: Neighbors indices 0 and 3 are in different islands. Total: 1+1+1 = 3.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.