Skip to content

Commit a1922ff

Browse files
authored
Merge pull request #1599 from Hitesh4278/find-all-groups-of-farmland
Added the Solution of Find all groups of farmland - Issue No 1519
2 parents 34c9c5c + 41bc0ef commit a1922ff

File tree

2 files changed

+352
-1
lines changed

2 files changed

+352
-1
lines changed

dsa-problems/leetcode-problems/1900-1999.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ export const problems = [
566566
"problemName": "1992. Find All Groups of Farmland",
567567
"difficulty": "Medium",
568568
"leetCodeLink": "https://leetcode.com/problems/find-all-groups-of-farmland",
569-
"solutionLink": "#"
569+
"solutionLink": "/dsa-solutions/lc-solutions/1900-1999/find-all-groups-of-farmland"
570570
},
571571
{
572572
"problemName": "1993. Operations on Tree",
Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
---
2+
id: find-all-groups-of-farmland
3+
title: Find All Groups of Farmland
4+
sidebar_label: 1992. Find All Groups of Farmland
5+
tags:
6+
- Array
7+
- Greedy
8+
- Heap (Priority Queue)
9+
10+
description: "This is a solution to the Remove Stones to Minimize the Total problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
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.
15+
16+
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.
17+
18+
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].
19+
20+
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.
21+
### Examples
22+
23+
**Example 1:**
24+
![image](https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-23-15-copy-of-diagram-drawio-diagrams-net.png)
25+
```
26+
Input: land = [[1,0,0],[0,1,1],[0,1,1]]
27+
Output: [[0,0,0,0],[1,1,2,2]]
28+
```
29+
30+
**Example 2:**
31+
![image](https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-30-26-copy-of-diagram-drawio-diagrams-net.png)
32+
33+
```
34+
Input: land = [[1,1],[1,1]]
35+
Output: [[0,0,1,1]]
36+
Explanation:
37+
The first group has a top left corner at land[0][0] and a bottom right corner at land[1][1].
38+
```
39+
40+
41+
### Constraints
42+
- `m == land.length`
43+
- `n == land[i].length`
44+
- `1 <= m, n <= 300`
45+
- land consists of only 0's and 1's.
46+
- Groups of farmland are rectangular in shape.
47+
48+
## Solution for Remove Stones to Minimize the Total Problem
49+
## Intuition
50+
51+
The problem is to find all the rectangular farmlands in a grid. Each cell in the grid is either part of a farmland (denoted by 1) or not (denoted by 0). Farmlands are connected horizontally or vertically and form rectangular shapes. The goal is to identify the top-left and bottom-right coordinates of each rectangular farmland.
52+
53+
## Approach
54+
55+
1. **Grid Traversal**:
56+
- Traverse each cell in the grid.
57+
- When encountering a cell with a value of 1 (indicating the start of a new farmland), initiate a Breadth-First Search (BFS) to find the extent of this farmland.
58+
59+
2. **Breadth-First Search (BFS)**:
60+
- Use a queue to explore all cells connected to the current farmland.
61+
- Track the maximum row and column indices (`eR` and `eC`) to determine the bottom-right corner of the farmland.
62+
- For each cell, check its four neighboring cells (up, down, left, right). If a neighboring cell is part of the farmland (i.e., its value is 1), add it to the queue and mark it as visited (set its value to 0).
63+
64+
3. **Record Farmland**:
65+
- After completing the BFS for a farmland, record its top-left and bottom-right coordinates.
66+
- Continue the grid traversal to find all farmlands.
67+
68+
<Tabs>
69+
<TabItem value="Solution" label="Solution">
70+
71+
#### Implementation
72+
```jsx live
73+
function Solution(arr) {
74+
function findFarmland(land) {
75+
const ans = [];
76+
const m = land.length;
77+
const n = land[0].length;
78+
const delRow = [-1, 0, 1, 0];
79+
const delCol = [0, 1, 0, -1];
80+
const q = [];
81+
82+
for (let i = 0; i < m; i++) {
83+
for (let j = 0; j < n; j++) {
84+
if (land[i][j] === 1) {
85+
q.push([i, j]);
86+
land[i][j] = 0;
87+
let eR = i, eC = j;
88+
while (q.length > 0) {
89+
const [sR, sC] = q.shift();
90+
91+
for (let k = 0; k < 4; k++) {
92+
const nR = sR + delRow[k];
93+
const nC = sC + delCol[k];
94+
if (nR >= 0 && nR < m && nC >= 0 && nC < n && land[nR][nC] === 1) {
95+
q.push([nR, nC]);
96+
land[nR][nC] = 0;
97+
eR = Math.max(eR, nR);
98+
eC = Math.max(eC, nC);
99+
}
100+
}
101+
}
102+
ans.push([i, j, eR, eC]);
103+
}
104+
}
105+
}
106+
return ans;
107+
}
108+
109+
const input = [[1,0,0],[0,1,1],[0,1,1]]
110+
111+
const output =findFarmland(input)
112+
return (
113+
<div>
114+
<p>
115+
<b>Input: </b>
116+
{JSON.stringify(input)}
117+
</p>
118+
<p>
119+
<b>Output:</b> {output.toString()}
120+
</p>
121+
</div>
122+
);
123+
}
124+
```
125+
126+
#### Complexity Analysis
127+
128+
- Time Complexity: $ O(n*m) $
129+
- Space Complexity: $ O(n*m) $
130+
131+
## Code in Different Languages
132+
<Tabs>
133+
<TabItem value="JavaScript" label="JavaScript">
134+
<SolutionAuthor name="@hiteshgahanolia"/>
135+
```javascript
136+
function findFarmland(land) {
137+
const ans = [];
138+
const m = land.length;
139+
const n = land[0].length;
140+
const delRow = [-1, 0, 1, 0];
141+
const delCol = [0, 1, 0, -1];
142+
const q = [];
143+
144+
for (let i = 0; i < m; i++) {
145+
for (let j = 0; j < n; j++) {
146+
if (land[i][j] === 1) {
147+
q.push([i, j]);
148+
land[i][j] = 0;
149+
let eR = i, eC = j;
150+
while (q.length > 0) {
151+
const [sR, sC] = q.shift();
152+
153+
for (let k = 0; k < 4; k++) {
154+
const nR = sR + delRow[k];
155+
const nC = sC + delCol[k];
156+
if (nR >= 0 && nR < m && nC >= 0 && nC < n && land[nR][nC] === 1) {
157+
q.push([nR, nC]);
158+
land[nR][nC] = 0;
159+
eR = Math.max(eR, nR);
160+
eC = Math.max(eC, nC);
161+
}
162+
}
163+
}
164+
ans.push([i, j, eR, eC]);
165+
}
166+
}
167+
}
168+
return ans;
169+
}
170+
171+
```
172+
173+
</TabItem>
174+
<TabItem value="TypeScript" label="TypeScript">
175+
<SolutionAuthor name="@hiteshgahanolia"/>
176+
```typescript
177+
function findFarmland(land: number[][]): number[][] {
178+
const ans: number[][] = [];
179+
const m = land.length;
180+
const n = land[0].length;
181+
const delRow = [-1, 0, 1, 0];
182+
const delCol = [0, 1, 0, -1];
183+
const q: [number, number][] = [];
184+
185+
for (let i = 0; i < m; i++) {
186+
for (let j = 0; j < n; j++) {
187+
if (land[i][j] === 1) {
188+
q.push([i, j]);
189+
land[i][j] = 0;
190+
let eR = i, eC = j;
191+
while (q.length > 0) {
192+
const [sR, sC] = q.shift()!;
193+
194+
for (let k = 0; k < 4; k++) {
195+
const nR = sR + delRow[k];
196+
const nC = sC + delCol[k];
197+
if (nR >= 0 && nR < m && nC >= 0 && nC < n && land[nR][nC] === 1) {
198+
q.push([nR, nC]);
199+
land[nR][nC] = 0;
200+
eR = Math.max(eR, nR);
201+
eC = Math.max(eC, nC);
202+
}
203+
}
204+
}
205+
ans.push([i, j, eR, eC]);
206+
}
207+
}
208+
}
209+
return ans;
210+
}
211+
212+
```
213+
</TabItem>
214+
<TabItem value="Python" label="Python">
215+
<SolutionAuthor name="@hiteshgahanolia"/>
216+
```python
217+
from collections import deque
218+
219+
class Solution:
220+
def findFarmland(self, land: List[List[int]]) -> List[List[int]]:
221+
ans = []
222+
m, n = len(land), len(land[0])
223+
delRow = [-1, 0, 1, 0]
224+
delCol = [0, 1, 0, -1]
225+
q = deque()
226+
227+
for i in range(m):
228+
for j in range(n):
229+
if land[i][j] == 1:
230+
q.append((i, j))
231+
land[i][j] = 0
232+
eR, eC = i, j
233+
while q:
234+
sR, sC = q.popleft()
235+
236+
for k in range(4):
237+
nR, nC = sR + delRow[k], sC + delCol[k]
238+
if 0 <= nR < m and 0 <= nC < n and land[nR][nC] == 1:
239+
q.append((nR, nC))
240+
land[nR][nC] = 0
241+
eR = max(eR, nR)
242+
eC = max(eC, nC)
243+
ans.append([i, j, eR, eC])
244+
return ans
245+
246+
```
247+
248+
</TabItem>
249+
<TabItem value="Java" label="Java">
250+
<SolutionAuthor name="@hiteshgahanolia"/>
251+
```java
252+
import java.util.*;
253+
254+
class Solution {
255+
public List<int[]> findFarmland(int[][] land) {
256+
List<int[]> ans = new ArrayList<>();
257+
Queue<int[]> q = new LinkedList<>();
258+
int m = land.length;
259+
int n = land[0].length;
260+
int[] delRow = {-1, 0, 1, 0};
261+
int[] delCol = {0, 1, 0, -1};
262+
263+
for (int i = 0; i < m; i++) {
264+
for (int j = 0; j < n; j++) {
265+
if (land[i][j] == 1) {
266+
q.add(new int[]{i, j});
267+
land[i][j] = 0;
268+
int eR = i, eC = j;
269+
while (!q.isEmpty()) {
270+
int[] cell = q.poll();
271+
int sR = cell[0];
272+
int sC = cell[1];
273+
274+
for (int k = 0; k < 4; k++) {
275+
int nR = sR + delRow[k];
276+
int nC = sC + delCol[k];
277+
if (nR >= 0 && nR < m && nC >= 0 && nC < n && land[nR][nC] == 1) {
278+
q.add(new int[]{nR, nC});
279+
land[nR][nC] = 0;
280+
eR = Math.max(eR, nR);
281+
eC = Math.max(eC, nC);
282+
}
283+
}
284+
}
285+
ans.add(new int[]{i, j, eR, eC});
286+
}
287+
}
288+
}
289+
return ans;
290+
}
291+
}
292+
293+
```
294+
295+
</TabItem>
296+
<TabItem value="C++" label="C++">
297+
<SolutionAuthor name="@hiteshgahanolia"/>
298+
```cpp
299+
class Solution {
300+
public:
301+
vector<vector<int>> findFarmland(vector<vector<int>>& land) {
302+
vector<vector<int>> ans;
303+
queue<pair<int, int>> q;
304+
int m = land.size();
305+
int n = land[0].size();
306+
int delRow[4] = {-1, 0, 1, 0};
307+
int delCol[4] = {0, 1, 0, -1};
308+
309+
for (int i = 0; i < m; i++) {
310+
for (int j = 0; j < n; j++) {
311+
if (land[i][j] == 1) {
312+
q.push({i, j});
313+
land[i][j] = 0;
314+
int eR = i, eC = j;
315+
while (!q.empty()) {
316+
int sR = q.front().first;
317+
int sC = q.front().second;
318+
q.pop();
319+
320+
for (int k = 0; k < 4; k++) {
321+
int nR = sR + delRow[k];
322+
int nC = sC + delCol[k];
323+
if (nR >= 0 && nR < m && nC >= 0 && nC < n && land[nR][nC] == 1) {
324+
q.push({nR, nC});
325+
land[nR][nC] = 0;
326+
eR = max(eR, nR);
327+
eC = max(eC, nC);
328+
}
329+
}
330+
}
331+
ans.push_back({i, j, eR, eC});
332+
}
333+
}
334+
}
335+
return ans;
336+
}
337+
};
338+
339+
```
340+
</TabItem>
341+
</Tabs>
342+
343+
</TabItem>
344+
</Tabs>
345+
346+
## References
347+
348+
- **LeetCode Problem**: [Find All Groups of Farmland](https://leetcode.com/problems/find-all-groups-of-farmland/description/)
349+
350+
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/find-all-groups-of-farmland/description/)
351+

0 commit comments

Comments
 (0)