All problems
0109HardArrayDepth-First SearchBreadth-First SearchUnion-FindMatrix

Plant One Bare Cell

Tracked in this browser only
Write code

Trains the technique from

LeetCode 827Making A Large Island

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

An orchard occupies a square plot laid out as the n x n grid field. A cell holds 1 where a tree already stands and 0 where the ground is still bare.

Two trees belong to the same stand when you can get from one to the other by stepping between trees that share a full side of their cells, moving up, down, left or right. Cells that merely touch at a corner are not linked. The size of a stand is the count of trees in it.

The grower may plant a tree on one bare cell, or may plant nothing at all. Return the size of the biggest stand the orchard can end up with after that single choice.

Examples

Example 1

Input
field = [[1, 1, 0], [0, 0, 1], [1, 0, 1]]
Output
5

Planting the bare cell in the top right corner joins the pair of trees along the top row with the pair down the right edge, giving a stand of five.

Example 2

Input
field = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
Output
9

Eight trees already ring a single bare cell in the middle, and planting it pulls the whole ring plus the new tree into one stand of nine.

Example 3

Input
field = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Output
3

The three trees only touch at corners, so none of them are linked. The best single planting bridges two of them for a stand of three.

Constraints

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

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def largest_planted_block(field: list[list[int]]) -> int:
Java
public int largestPlantedBlock(int[][] field)
September 7
Apply