LeetCode: Max Area Of Island Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var maxAreaOfIsland = function (grid) {2  const [m, n] = [grid.length, grid[0].length]3  const visited = Array.from({ length: m }, _ => Array(n).fill(false))4
5  const valid = (row, col) => {6    return (7      row >= 0 &&8      row < m &&9      col >= 0 &&10      col < n &&11      !visited[row][col] &&12      grid[row][col] !== 013    )14  }15
16  const traverse = (row, col) => {17    if (!valid(row, col)) {18      return 019    }20
21    visited[row][col] = true22
23    return (24      1 +25      traverse(row, col + 1) +26      traverse(row, col - 1) +27      traverse(row + 1, col) +28      traverse(row - 1, col)29    )30  }31
32  let res = 033  for (let row = 0; row < m; row++) {34    for (let col = 0; col < n; col++) {35      res = Math.max(res, traverse(row, col))36    }37  }38
39  return res40}```

leetcode

array

matrix

dfs

Next Post

CSSBattle 1.4: Ups n Downs

Jun 3, 2021

Search Posts