In this assignment, your task is to solve the problem "Largest Forest Area." Try to solve the problem independently, and you can always reference our walkthrough if you need help.
// You are provided with an 'm x n' 2D grid map where each cell
// is either marked as a tree ('T') or open land ('O'). Your task
// is to find the largest contiguous forest area on the map. A
// forest area consists of a group of tree cells ('T') connected
// 4-directionally (horizontally or vertically, but not diagonally).
// Write a function largestForestArea that accepts a nested
// array grid representing the 2D map. The function should
// return the size of the largest forest area, which is the
// number of contiguous 'T' cells in the largest forest.
// If there are no trees in the grid, return 0.
// Example:
// Input:
// [
// ['O', 'T', 'O', 'O'],
// ['T', 'T', 'O', 'T'],
// ['O', 'O', 'O', 'T'],
// ['O', 'O', 'T', 'T']
// ]
// Output: 4 (The largest forest area has 4 connected tree cells)
function largestForestArea(grid) {
// Implementation goes here
}
// Test cases
const grid1 = [];
console.log(largestForestArea(grid1) === 0);
const grid2 = [
['O', 'O', 'O'],
['O', 'O', 'O'],
['O', 'O', 'O']
];
console.log(largestForestArea(grid2) === 0);
const grid3 = [
['T', 'T', 'O'],
['T', 'T', 'O'],
['O', 'O', 'O']
];
console.log(largestForestArea(grid3) === 4);
const grid4 = [
['O', 'O', 'T', 'T', 'O'],
['T', 'T', 'O', 'T', 'O'],
['T', 'T', 'O', 'O', 'O'],
['O', 'O', 'O', 'T', 'T'],
['O', 'O', 'O', 'O', 'O'],
];
console.log(largestForestArea(grid4) === 4);
const grid5 = [
['T', 'T', 'T'],
['T', 'O', 'T'],
['T', 'T', 'T']
];
console.log(largestForestArea(grid5) === 8);
const grid6 = [
['T', 'O', 'T', 'O', 'T'],
['O', 'O', 'O', 'O', 'O'],
['T', 'O', 'T', 'O', 'T'],
['O', 'O', 'O', 'O', 'O'],
['T', 'O', 'T', 'O', 'T']
];
console.log(largestForestArea(grid6) === 1);
const grid7 = [
['T', 'T', 'T'],
['T', 'T', 'T'],
['T', 'T', 'T']
];
console.log(largestForestArea(grid7) === 9);
const grid8 = [
['O', 'T', 'O', 'T', 'T'],
['O', 'T', 'O', 'O', 'O'],
['O', 'O', 'T', 'O', 'O'],
['O', 'O', 'T', 'T', 'T'],
['T', 'O', 'T', 'T', 'T']
];
console.log(largestForestArea(grid8) === 7);
const grid9 = [
['T', 'O', 'T', 'T'],
['O', 'T', 'O', 'T'],
['T', 'T', 'O', 'O'],
['O', 'T', 'T', 'T']
];
console.log(largestForestArea(grid9) === 6);
const grid10 = [
['O', 'T', 'O', 'O'],
['T', 'T', 'O', 'T'],
['O', 'O', 'O', 'T'],
['O', 'O', 'T', 'T']
];
console.log(largestForestArea(grid10) === 4);
const grid11 = [
['O', 'T', 'T', 'T', 'O'],
['T', 'T', 'O', 'T', 'T'],
['O', 'O', 'O', 'O', 'O'],
['T', 'T', 'O', 'T', 'O'],
['T', 'T', 'O', 'T', 'T']
];
console.log(largestForestArea(grid11) === 7);
// All test cases should log true
The key difference from the previous problem is that we're measuring their size instead of counting forests.
In the previous problem, we saw two solutions: one with a set and one without. In this problem, we could also use a set to mark squares as visited.
However, an even better solution is simply converting the tree cell to open land during the depth-first search phase. That way, we wouldn't ever encounter a tree cell that has already been visited when iterating through our rows of cells.
Let's outline the algorithm for the Largest Forest Area problem:
Initialize variables
largestForest
to to keep track of the size of the largest forest found so far.
Iterate through the grid
Perform Depth-First Search (DFS)
currentForestSize
to 0
.
currentForestSize
.
Update the largest forest size
currentForestSize
with largestForest
.
currentForestSize
is larger, update largestForest
.
Return result
largestForest
.
Let's do a walkthrough on one of the examples from the test cases:
(0,1)
. We perform a depth-first search and mark it and its connected tree (1,1)
as visited by converting it to an open land cell. We count the size of this forest (two trees) and set it as our current largest forest.
(0,3)
.
(0,3)
, marking it and (0,4)
as visited by converting them to open land cells. This forest also has a size of two trees, which equals our current largest forest, so we don't update the largest forest size.
(2,2)
. We perform depth-first search to explore that forest. We mark (2,2)
, (3,2)
, (3,3)
, (3,4)
, (4,2)
, (4,3)
, and (4,4)
as visited by converting them to open land cells. This forest has a size of seven trees, which is larger than our current largest forest, so we update the largest forest size to seven.
(4,0)
.
function largestForestArea(grid) {
if (!grid || grid.length === 0) return 0;
const numRows = grid.length;
const numCols = grid[0].length;
let largestForest = 0;
let currentForestSize = 0;
function dfs(row, col) {
if (
row < 0 || row >= numRows || col < 0 || col >= numCols ||
grid[row][col] === 'O'
) {
return;
}
grid[row][col] = 'O';
currentForestSize++;
dfs(row, col - 1);
dfs(row - 1, col);
dfs(row, col + 1);
dfs(row + 1, col);
}
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
if (grid[row][col] === 'T') {
currentForestSize = 0;
dfs(row, col);
largestForest = Math.max(largestForest, currentForestSize);
}
}
}
return largestForest;
}