Skip to content

Commit f9d3036

Browse files
add 1992
1 parent b614e8d commit f9d3036

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
public class _1992 {
9+
public static class Solution1 {
10+
public int[][] findFarmland(int[][] land) {
11+
List<int[]> farms = new ArrayList<>();
12+
int m = land.length;
13+
int n = land[0].length;
14+
boolean[][] visited = new boolean[m][n];
15+
int[] directions = new int[]{0, 1, 0, -1, 0};
16+
for (int i = 0; i < m; i++) {
17+
for (int j = 0; j < n; j++) {
18+
if (land[i][j] == 1 && !visited[i][j]) {
19+
visited[i][j] = true;
20+
Queue<int[]> queue = new LinkedList<>();
21+
queue.offer(new int[]{i, j});
22+
int[] coords = new int[]{i, j, i, j};
23+
while (!queue.isEmpty()) {
24+
int size = queue.size();
25+
for (int k = 0; k < size; k++) {
26+
int[] curr = queue.poll();
27+
for (int p = 0; p < directions.length - 1; p++) {
28+
int newX = directions[p] + curr[0];
29+
int newY = directions[p + 1] + curr[1];
30+
if (newX >= 0 && newY >= 0 && newX < m && newY < n && land[newX][newY] == 1 && !visited[newX][newY]) {
31+
visited[newX][newY] = true;
32+
queue.offer(new int[]{newX, newY});
33+
coords[0] = Math.min(coords[0], newX);
34+
coords[1] = Math.min(coords[1], newY);
35+
coords[2] = Math.max(coords[2], newX);
36+
coords[3] = Math.max(coords[3], newY);
37+
}
38+
}
39+
}
40+
}
41+
farms.add(coords);
42+
}
43+
}
44+
}
45+
int[][] result = new int[farms.size()][4];
46+
for (int i = 0; i < farms.size(); i++) {
47+
for (int j = 0; j < farms.get(i).length; j++) {
48+
result[i][j] = farms.get(i)[j];
49+
}
50+
}
51+
return result;
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1992;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1992Test {
11+
private static _1992.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1992.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(new int[][]{{13, 1, 28, 1}, {22, 4, 24, 39}}, solution1.findFarmland(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("" +
21+
"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," +//0
22+
"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," +//1
23+
"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," +//2
24+
"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," +//3
25+
"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," +//4
26+
"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," +//5
27+
"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," +//6
28+
"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," +//7
29+
"[0,0,0,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