Graph Theory37 sections · 1633 units
Open in Course

LeetCode 827 Making A Large Island - Logic

DSU + Virtual Flip

1.1. Build: Loop through the grid.

If a cell is 11, unite it with its 11 neighbors. Use DSU with Size Tracking.

2.2. Search: Now loop through every 00 cell.

3.3. Test: Imagine turning this 00 into a 11. It would connect its 44 neighbors.

4.4. Sum: Look up the Leader of each neighbor. Sum the sizes of these unique leaders + 11 (for the new cell).

5.5. The answer is the maximum sum you find.

Note: Be careful not to double-count if Up and Down already belong to the same island!