LeetCode Problem

How to Solve Making A Large Island

This problem requires finding the maximum island area by flipping at most one 0 to 1 in a binary grid. Using Depth-First Search or Breadth-First Search, you can label each island and track their sizes. By examining all zeros and connecting neighboring islands, the optimal change can be determined efficiently.

GhostInterview Help

Need help with Making A Large Island without spending extra time grinding it?

GhostInterview can read Making A Large Island from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #827Array plus Depth-First SearchReviewed 2026-03-07
Difficulty
Hard
Primary pattern
Array plus Depth-First Search
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem requires finding the maximum island area by flipping at most one 0 to 1 in a binary grid. Using Depth-First Search or Breadth-First Search, you can label each island and track their sizes. By examining all zeros and connecting neighboring islands, the optimal change can be determined efficiently.

Problem Statement

Given an n x n binary matrix grid, you may flip at most one 0 to a 1. An island is defined as a group of 1s connected 4-directionally. Your task is to determine the largest possible island area after performing at most one flip.

Return the maximum size of an island obtainable after flipping one zero. If the grid is already full of 1s, return the total number of cells. Constraints include 1 <= n <= 500 and each grid cell being 0 or 1.

Examples

Example 1

Input: grid = [[1,0],[0,1]]

Output: 3

Change one 0 to 1 and connect two 1s, then we get an island with area = 3.

Example 2

Input: grid = [[1,1],[1,0]]

Output: 4

Change the 0 to 1 and make the island bigger, only one island with area = 4.

Example 3

Input: grid = [[1,1],[1,1]]

Output: 4

Can't change any 0 to 1, only one island with area = 4.

Constraints

  • n == grid.length
  • n == grid[i].length
  • 1 <= n <= 500
  • grid[i][j] is either 0 or 1.

Solution Approach

Label islands with DFS

Perform a Depth-First Search to traverse the grid and assign a unique index to each island while recording its size. Store these sizes in a map for quick access when evaluating potential flips.

Evaluate each zero cell

For every zero in the grid, check its 4-directional neighbors and collect the indices of distinct adjacent islands. Summing their sizes plus one gives the potential new island size if that zero is flipped.

Compute maximum island

Iterate through all zeros and calculate the connected island sizes using the map from DFS labeling. Track the maximum encountered value and return it as the largest possible island area.

Complexity Analysis

MetricValue
TimeO(n \times m)
SpaceO(n \times m)

DFS labeling takes O(nm) time and O(nm) space to store visited states and island sizes. Evaluating each zero requires checking at most 4 neighbors, keeping the total time complexity within O(n*m).

What Interviewers Usually Probe

  • Checking DFS versus BFS trade-offs is important for large grids.
  • Remember to handle grids fully filled with 1s where no flips are needed.
  • Be ready to explain how using a map for island sizes avoids repeated recalculations.

Common Pitfalls or Variants

Common pitfalls

  • Counting the same island twice when multiple zeros share neighbors.
  • Not labeling islands correctly, leading to incorrect size computation.
  • Forgetting the edge case where no zeros exist, requiring total grid count.

Follow-up variants

  • Allow flipping multiple zeros and find maximum combined island size.
  • Use Union-Find instead of DFS to track connected components dynamically.
  • Compute largest island size when diagonal connections are also considered.

How GhostInterview Helps

  • Provides step-by-step DFS labeling guidance specific to Making A Large Island.
  • Highlights exactly how to merge neighboring islands when flipping a zero.
  • Tracks edge cases like fully filled grids and avoids common counting mistakes.

Topic Pages

FAQ

What is the best approach pattern for Making A Large Island?

Using an array-based DFS pattern to label islands and map their sizes is the most direct method to determine the maximum area after one flip.

How do I handle a grid with no zeros?

If the grid has no zeros, the largest island is the entire grid, so simply return n*n without further computation.

Can BFS be used instead of DFS?

Yes, BFS can replace DFS for labeling islands, but DFS often provides simpler recursive implementation for tracking island sizes.

How should I avoid double-counting islands when flipping a zero?

Use a set to collect unique neighboring island indices before summing their sizes to prevent counting the same island multiple times.

What are the space and time considerations for large grids?

Both DFS and BFS require O(nm) space for visited cells and island sizes, and total time complexity remains O(nm), suitable for grids up to 500x500.

GhostInterview Solver

Need direct help with Making A Large Island instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Making A Large Island from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.