LeetCode 1992: Find All Groups of Farmland Solution
Master LeetCode problem 1992 (Find All Groups of Farmland), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1992. Find All Groups of Farmland
Problem Explanation
Explanation:
To solve this problem, we can perform a depth-first search (DFS) on the given matrix land. We iterate through each cell in the matrix, and when we encounter a cell with the value 1, we start a DFS from that cell to find the coordinates of the farmland group. During the DFS, we mark visited cells and keep track of the top left and bottom right corners of the group.
Algorithm:
- Initialize an empty list to store the result.
- Iterate through each cell in the matrix:
- If the cell value is
1and it has not been visited:- Start a DFS from that cell.
- During the DFS, update the top left and bottom right corners of the group.
- If the cell value is
- Return the list of coordinates of the farmland groups.
Time Complexity:
The time complexity of this approach is O(m * n), where m is the number of rows and n is the number of columns in the matrix.
Space Complexity:
The space complexity is O(m * n) due to the recursion stack during the DFS.
: :
Solution Code
class Solution {
public int[][] findFarmland(int[][] land) {
List<int[]> result = new ArrayList<>();
int m = land.length;
int n = land[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (land[i][j] == 1) {
int[] corners = new int[]{i, j, i, j};
dfs(land, i, j, corners);
result.add(corners);
}
}
}
return result.toArray(new int[0][]);
}
private void dfs(int[][] land, int row, int col, int[] corners) {
if (row < 0 || row >= land.length || col < 0 || col >= land[0].length || land[row][col] == 0) {
return;
}
land[row][col] = 0; // Mark as visited
corners[2] = Math.max(corners[2], row); // Update bottom right row
corners[3] = Math.max(corners[3], col); // Update bottom right col
dfs(land, row + 1, col, corners); // Down
dfs(land, row - 1, col, corners); // Up
dfs(land, row, col + 1, corners); // Right
dfs(land, row, col - 1, corners); // Left
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1992 (Find All Groups of Farmland)?
This page provides optimized solutions for LeetCode problem 1992 (Find All Groups of Farmland) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1992 (Find All Groups of Farmland)?
The time complexity for LeetCode 1992 (Find All Groups of Farmland) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1992 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1992 in Java, C++, or Python.