Find All Groups of Farmland LeetCode solution in JavaScript

1
var findFarmland = function (land) {
2
let height = land.length;
3
let width = land[0].length;
4
let results = [];
5
let endRow = 0;
6
let endCol = 0;
7

8
let go = (i, j) => {
9
if (i < 0 || j < 0 || i >= height || j >= width || land[i][j] === 0) {
10
return;
11
}
12

13
endRow = Math.max(endRow, i);
14
endCol = Math.max(endCol, j);
15
land[i][j] = 0; // reset everything to 0
16

17
go(i + 1, j);
18
go(i - 1, j);
19
go(i, j + 1);
20
go(i, j - 1);
21
};
22

23
for (let i = 0; i < height; i++) {
24
for (let j = 0; j < width; j++) {
25
if (land[i][j] === 1) {
26
endRow = 0;
27
endCol = 0;
28
go(i, j);
29
results.push([i, j, endRow, endCol]);
30
}
31
}
32
}
33

34
return results;
35
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0