Skip to content

Commit c5fc936

Browse files
authored
Added task 1992.
1 parent f8878de commit c5fc936

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g1901_2000.s1992_find_all_groups_of_farmland;
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
// #2022_05_19_Time_7_ms_(89.08%)_Space_52.1_MB_(87.32%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private final List<int[]> res = new ArrayList<>();
11+
12+
public int[][] findFarmland(int[][] land) {
13+
if (land == null || land.length == 0) {
14+
return new int[][] {};
15+
}
16+
int m = land.length;
17+
int n = land[0].length;
18+
for (int i = 0; i < m; i++) {
19+
for (int j = 0; j < n; j++) {
20+
if (land[i][j] == 1) {
21+
int[] dirs = new int[4];
22+
dirs[0] = i;
23+
dirs[1] = j;
24+
dirs[2] = i;
25+
dirs[3] = j;
26+
dfs(land, i, j, dirs);
27+
res.add(dirs);
28+
}
29+
}
30+
}
31+
return res.toArray(new int[0][]);
32+
}
33+
34+
private void dfs(int[][] land, int i, int j, int[] dirs) {
35+
if (i < 0 || i >= land.length || j < 0 || j >= land[0].length || land[i][j] != 1) {
36+
return;
37+
}
38+
land[i][j] = -1;
39+
dfs(land, i + 1, j, dirs);
40+
dfs(land, i, j + 1, dirs);
41+
dirs[0] = Math.min(dirs[0], i);
42+
dirs[1] = Math.min(dirs[1], j);
43+
dirs[2] = Math.max(dirs[2], i);
44+
dirs[3] = Math.max(dirs[3], j);
45+
}
46+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1992\. Find All Groups of Farmland
2+
3+
Medium
4+
5+
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.
6+
7+
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.
8+
9+
`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 <code>(r<sub>1</sub>, c<sub>1</sub>)</code> and a bottom right corner at <code>(r<sub>2</sub>, c<sub>2</sub>)</code> is represented by the 4-length array <code>[r<sub>1</sub>, c<sub>1</sub>, r<sub>2</sub>, c<sub>2</sub>].</code>
10+
11+
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**_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-23-15-copy-of-diagram-drawio-diagrams-net.png)
16+
17+
**Input:** land = [[1,0,0],[0,1,1],[0,1,1]]
18+
19+
**Output:** [[0,0,0,0],[1,1,2,2]]
20+
21+
**Explanation:**
22+
23+
The first group has a top left corner at land[0][0] and a bottom right corner at land[0][0].
24+
25+
The second group has a top left corner at land[1][1] and a bottom right corner at land[2][2].
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-30-26-copy-of-diagram-drawio-diagrams-net.png)
30+
31+
**Input:** land = [[1,1],[1,1]]
32+
33+
**Output:** [[0,0,1,1]]
34+
35+
**Explanation:**
36+
37+
The first group has a top left corner at land[0][0] and a bottom right corner at land[1][1].
38+
39+
**Example 3:**
40+
41+
![](https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-32-24-copy-of-diagram-drawio-diagrams-net.png)
42+
43+
**Input:** land = [[0]]
44+
45+
**Output:** []
46+
47+
**Explanation:** There are no groups of farmland.
48+
49+
**Constraints:**
50+
51+
* `m == land.length`
52+
* `n == land[i].length`
53+
* `1 <= m, n <= 300`
54+
* `land` consists of only `0`'s and `1`'s.
55+
* Groups of farmland are **rectangular** in shape.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g1901_2000.s1992_find_all_groups_of_farmland;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findFarmland() {
11+
assertThat(
12+
new Solution().findFarmland(new int[][] {{1, 0, 0}, {0, 1, 1}, {0, 1, 1}}),
13+
equalTo(new int[][] {{0, 0, 0, 0}, {1, 1, 2, 2}}));
14+
}
15+
16+
@Test
17+
void findFarmland2() {
18+
assertThat(
19+
new Solution().findFarmland(new int[][] {{1, 1}, {1, 1}}),
20+
equalTo(new int[][] {{0, 0, 1, 1}}));
21+
}
22+
23+
@Test
24+
void findFarmland3() {
25+
assertThat(new Solution().findFarmland(new int[][] {{0}}), equalTo(new int[][] {}));
26+
}
27+
28+
@Test
29+
void findFarmland4() {
30+
assertThat(new Solution().findFarmland(new int[][] {}), equalTo(new int[][] {}));
31+
}
32+
33+
@Test
34+
void findFarmland5() {
35+
assertThat(new Solution().findFarmland(null), equalTo(new int[][] {}));
36+
}
37+
}

0 commit comments

Comments
 (0)