1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/rs/find-all-groups-of-farmland.rs
Matej Focko 48cdf36d0f
rs: add «1992. Find All Groups of Farmland»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-04-20 16:31:52 +02:00

51 lines
1.4 KiB
Rust

struct Solution {}
impl Solution {
fn dimensions(grid: &[Vec<i32>]) -> (usize, usize) {
(grid.len(), grid[0].len())
}
fn indices(m: usize, n: usize) -> impl Iterator<Item = (usize, usize)> {
(0..m).flat_map(move |y| (0..n).map(move |x| (x, y)))
}
fn find(land: &[Vec<i32>], visited: &mut [Vec<bool>], min_x: usize, min_y: usize) -> Vec<i32> {
// horizontally
let mut max_x = min_x;
while max_x < land[min_y].len() && land[min_y][max_x] == 1 {
max_y += 1;
}
// vertically
let mut max_y = min_y;
while max_y < land.len() && land[max_y][min_x] == 1 {
max_y += 1;
}
// mark all as visited
for (x, y) in (min_y..max_y).flat_map(move |y| (min_x..max_x).map(move |x| (x, y))) {
visited[y][x] = true;
}
vec![min_y, min_x, max_y - 1, max_x - 1]
.into_iter()
.map(|c| c as i32)
.collect()
}
pub fn find_farmland(land: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let (m, n) = Self::dimensions(&land);
let mut farmland: Vec<Vec<i32>> = Vec::new();
let mut visited = vec![vec![false; n]; m];
for (x, y) in Self::indices(m, n) {
if land[y][x] == 0 || visited[y][x] {
continue;
}
farmland.push(Self::find(&land, &mut visited, x, y));
}
farmland
}
}