#1992
Find All Groups of Farmland
pupil · 490 · lc medium +27 · premium · verified · 75.5% accepted · 1,442 likes · top 86%
Description
You have a 0-indexed m x n binary matrix land where 0 is forested land and 1 is farmland. Farmland forms non-overlapping rectangles, and no two rectangles are four-directionally adjacent.
Using a coordinate system where the top-left cell is (0, 0) and the bottom-right is (m-1, n-1), identify each rectangular farmland group by its top-left corner (r1, c1) and bottom-right corner (r2, c2), and represent it as [r1, c1, r2, c2].
Return all such 4-element arrays in any order, or an empty array if no farmland exists.
Example 1:
Input: land = [[1,0,0],[0,1,1],[0,1,1]]
Output: [[0,0,0,0],[1,1,2,2]]
Explanation:
The first group has a top left corner at land[0][0] and a bottom right corner at land[0][0].
The second group has a top left corner at land[1][1] and a bottom right corner at land[2][2].
Example 2:
Input: land = [[1,1],[1,1]]
Output: [[0,0,1,1]]
Explanation:
The first group has a top left corner at land[0][0] and a bottom right corner at land[1][1].
Example 3:
Input: land = [[0]]
Output: []
Explanation:
There are no groups of farmland.
Code
1
2
3