Find All Groups of Farmland LeetCode solution in JavaScript
1var findFarmland = function (land) {2let height = land.length;3let width = land[0].length;4let results = [];5let endRow = 0;6let endCol = 0;78let go = (i, j) => {9if (i < 0 || j < 0 || i >= height || j >= width || land[i][j] === 0) {10return;11}1213endRow = Math.max(endRow, i);14endCol = Math.max(endCol, j);15land[i][j] = 0; // reset everything to 01617go(i + 1, j);18go(i - 1, j);19go(i, j + 1);20go(i, j - 1);21};2223for (let i = 0; i < height; i++) {24for (let j = 0; j < width; j++) {25if (land[i][j] === 1) {26endRow = 0;27endCol = 0;28go(i, j);29results.push([i, j, endRow, endCol]);30}31}32}3334return results;35};