← 返回 uber 的题目列表Making a Large Island
类型:qbank
Given an n x n binary matrix, you may flip at most one 0 to 1. Return the size of the largest 4-directionally connected island of 1s achievable after the flip.
Making a Large Island
Given an n x n binary matrix, you may flip at most one 0 to 1. Return the size of the largest 4-directionally connected island of 1s achievable after the flip.
SWE
grid
dfs
union-find
hard
Frequency
Single report
Last asked
2026-01-07
Stage
oa
Making a Large Island
You are given an n x n binary matrix grid. You may change at most one 0 to 1.
Return the size of the largest island after applying this operation.
An island is a 4-directionally connected group of 1s.
Examples
Example 1:
Input: grid = [[1,0],[0,1]]
Output: 3
Explanation:
Flip either 0 to connect three cells into one island.
Example 2:
Input: grid = [[1,1],[1,0]]
Output: 4
Explanation:
Flip the bottom-right 0 to make the whole grid one island.
Example 3:
Input: grid = [[1,1],[1,1]]
Output: 4
Explanation:
The grid is already one island, so no flip is needed.
Constraints
n == grid.length
n == grid[i].length
1 <= n <= 500
grid[i][j] is either 0 or 1