Skip to content

Commit 00abb91

Browse files
committed
Leetcode | Find All Groups of Farmland | Kotlin
1 parent adf269f commit 00abb91

3 files changed

Lines changed: 122 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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.devstromo.medium.p1992
2+
3+
import kotlin.collections.toTypedArray
4+
5+
class SolutionKt {
6+
fun findFarmland(land: Array<IntArray>): Array<IntArray> {
7+
val m = land.size
8+
val n = land[0].size
9+
val result = mutableListOf<IntArray>()
10+
11+
for (r in 0 until m) {
12+
for (c in 0 until n) {
13+
if (land[r][c] == 1) {
14+
val bounds = intArrayOf(r, c, r, c)
15+
dfs(land, r, c, bounds)
16+
result.add(bounds)
17+
}
18+
}
19+
}
20+
21+
return result.toTypedArray()
22+
}
23+
24+
private fun dfs(land: Array<IntArray>, r: Int, c: Int, bounds: IntArray) {
25+
val m = land.size
26+
val n = land[0].size
27+
28+
if (r !in 0 until m || c !in 0 until n || land[r][c] != 1) return
29+
30+
land[r][c] = -1 // Mark as visited
31+
32+
bounds[2] = maxOf(bounds[2], r)
33+
bounds[3] = maxOf(bounds[3], c)
34+
35+
dfs(land, r + 1, c, bounds)
36+
dfs(land, r - 1, c, bounds)
37+
dfs(land, r, c + 1, bounds)
38+
dfs(land, r, c - 1, bounds)
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.devstromo.medium.p1992
2+
3+
import org.junit.jupiter.api.Assertions.assertArrayEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class SolutionKtUnitTest {
7+
private val solution = SolutionKt()
8+
9+
@Test
10+
fun `Test Find All Groups of Farmland`() {
11+
assertArrayEquals(
12+
arrayOf<IntArray?>(
13+
intArrayOf(0, 0, 0, 0),
14+
intArrayOf(1, 1, 2, 2),
15+
),
16+
solution.findFarmland(
17+
arrayOf(
18+
intArrayOf(1, 0, 0),
19+
intArrayOf(0, 1, 1),
20+
intArrayOf(0, 1, 1)
21+
)
22+
)
23+
)
24+
assertArrayEquals(
25+
arrayOf<IntArray?>(
26+
intArrayOf(0, 0, 1, 1)
27+
),
28+
solution.findFarmland(
29+
arrayOf(
30+
intArrayOf(1, 1),
31+
intArrayOf(1, 1)
32+
)
33+
)
34+
)
35+
assertArrayEquals(
36+
arrayOf<IntArray?>(
37+
intArrayOf(0)
38+
),
39+
solution.findFarmland(arrayOf())
40+
)
41+
}
42+
43+
}

0 commit comments

Comments
 (0)