Skip to main content
Back to problems
Leetcode
Medium
Arrays
Matrices
Greedy
Find All Groups Of Farmland

Find every rectangular farmland group in a binary grid and return the coordinates of its top-left and bottom-right corners.

Acceptance 0%
Problem Statement

You are given a 2D binary grid where 1 represents farmland and 0 represents empty land. Each connected group of farmland forms one solid axis-aligned rectangle, and different rectangles do not touch each other horizontally or vertically.

Your task is to identify every farmland group and return the coordinates of its top-left and bottom-right cells.

Return the result in any order.

Input Format

  • A 2D integer grid land of size m x n.
  • land[i][j] is either 0 or 1.
  • Each group of 1s forms a single rectangle.

Output Format

  • Return a list of rectangles.
  • For each rectangle, output [topRow, leftCol, bottomRow, rightCol].
  • The rectangles may be returned in any order.

Constraints

  • 1 <= m, n <= 300
  • land[i][j] is 0 or 1
  • Farmland groups are disjoint and each is a perfect rectangle.
Examples
Sample cases returned by the problem API.

Example 1

Input

land = [[1,0,0],[0,1,1],[0,1,1]]

Output

[[0,0,0,0],[1,1,2,2]]

Explanation

There are two farmland rectangles: one single cell at the top-left, and one 2x2 block in the bottom-right.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.