Skip to content

Commit 9f99641

Browse files
committed
Leetcode | Find All Groups of Farmland | Java
1 parent c2d85d3 commit 9f99641

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 1992. Find All Groups of Farmland
2+
3+
You are given a 0-indexed `m x n` binary matrix `land` where a `0` represents a hectare of forested land and a 1 represents a hectare of farmland.
4+
5+
To keep the land organized, there are designated rectangular areas of hectares that consist entirely of farmland. These rectangular areas are called groups. No two groups are adjacent, meaning farmland in one group is not four-directionally adjacent to another farmland in a different group.
6+
7+
`land` can be represented by a coordinate system where the top left corner of `land` is `(0, 0)` and the bottom right corner of `land` is `(m-1, n-1)`. Find the coordinates of the top left and bottom right corner of each group of farmland. A group of farmland with a top left corner at `(r1, c1)` and a bottom right corner at `(r2, c2)` is represented by the 4-length array `[r1, c1, r2, c2]`.
8+
9+
Return a 2D array containing the 4-length arrays described above for each group of farmland in `land`. If there are no groups of farmland, return an empty array. You may return the answer in any order.
10+
11+
Example 1:
12+
13+
Input: land = [[1,0,0],[0,1,1],[0,1,1]]
14+
Output: [[0,0,0,0],[1,1,2,2]]
15+
Explanation:
16+
The first group has a top left corner at land[0][0] and a bottom right corner at land[0][0].
17+
The second group has a top left corner at land[1][1] and a bottom right corner at land[2][2].
18+
19+
Example 2:
20+
21+
Input: land = [[1,1],[1,1]]
22+
Output: [[0,0,1,1]]
23+
Explanation:
24+
The first group has a top left corner at land[0][0] and a bottom right corner at land[1][1].
25+
26+
Example 3:
27+
28+
Input: land = [[0]]
29+
Output: []
30+
Explanation:
31+
There are no groups of farmland.
32+
33+
Constraints:
34+
35+
- `m == land.length`
36+
- `n == land[i].length`
37+
- `1 <= m, n <= 300`
38+
- `land` consists of only `0`'s and `1`'s.
39+
- Groups of farmland are **rectangular** in shape.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.devstromo.medium.p1992;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Solution {
7+
public int[][] findFarmland(int[][] land) {
8+
if (land.length == 0 || land[0].length == 0) {
9+
return new int[][]{
10+
{0}
11+
};
12+
}
13+
int m = land.length, n = land[0].length;
14+
List<int[]> result = new ArrayList<>();
15+
16+
// Visit all unvisited farmland cells (value == 1)
17+
for (int r = 0; r < m; r++) {
18+
for (int c = 0; c < n; c++) {
19+
if (land[r][c] == 1) {
20+
int[] bounds = new int[]{r, c, r, c};
21+
dfs(land, r, c, bounds);
22+
result.add(bounds);
23+
}
24+
}
25+
}
26+
27+
return result.toArray(new int[result.size()][]);
28+
}
29+
30+
private void dfs(int[][] land, int r, int c, int[] bounds) {
31+
int m = land.length, n = land[0].length;
32+
// Base case: out of bounds or not farmland
33+
if (r < 0 || r >= m || c < 0 || c >= n || land[r][c] != 1) return;
34+
35+
// Mark as visited
36+
land[r][c] = -1;
37+
38+
// Update the bottom-right corner
39+
bounds[2] = Math.max(bounds[2], r);
40+
bounds[3] = Math.max(bounds[3], c);
41+
42+
// Explore 4 directions
43+
dfs(land, r + 1, c, bounds);
44+
dfs(land, r - 1, c, bounds);
45+
dfs(land, r, c + 1, bounds);
46+
dfs(land, r, c - 1, bounds);
47+
}
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.devstromo.medium.p1992;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class SolutionUnitTest {
9+
private final Solution solution = new Solution();
10+
11+
@Test
12+
@DisplayName("Test Find All Groups of Farmland")
13+
void testFindAllGroupsOfFarmland() {
14+
assertArrayEquals(
15+
new int[][]{
16+
{0, 0, 0, 0},
17+
{1, 1, 2, 2},
18+
},
19+
solution.findFarmland(new int[][]{
20+
{1, 0, 0},
21+
{0, 1, 1},
22+
{0, 1, 1}
23+
})
24+
);
25+
assertArrayEquals(
26+
new int[][]{
27+
{0, 0, 1, 1}
28+
},
29+
solution.findFarmland(new int[][]{
30+
{1, 1},
31+
{1, 1}
32+
})
33+
);
34+
assertArrayEquals(
35+
new int[][]{
36+
{0}
37+
},
38+
solution.findFarmland(new int[][]{})
39+
);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)