返回介绍

solution / 0600-0699 / 0695.Max Area of Island / README_EN

发布于 2024-06-17 01:03:35 字数 6213 浏览 0 评论 0 收藏 0

695. Max Area of Island

中文文档

Description

You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

The area of an island is the number of cells with a value 1 in the island.

Return _the maximum area of an island in _grid. If there is no island, return 0.

 

Example 1:

Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output: 6
Explanation: The answer is not 11, because the island must be connected 4-directionally.

Example 2:

Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • grid[i][j] is either 0 or 1.

Solutions

Solution 1

class Solution:
  def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
    def dfs(i: int, j: int) -> int:
      if grid[i][j] == 0:
        return 0
      ans = 1
      grid[i][j] = 0
      dirs = (-1, 0, 1, 0, -1)
      for a, b in pairwise(dirs):
        x, y = i + a, j + b
        if 0 <= x < m and 0 <= y < n:
          ans += dfs(x, y)
      return ans

    m, n = len(grid), len(grid[0])
    return max(dfs(i, j) for i in range(m) for j in range(n))
class Solution {
  private int m;
  private int n;
  private int[][] grid;

  public int maxAreaOfIsland(int[][] grid) {
    m = grid.length;
    n = grid[0].length;
    this.grid = grid;
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        ans = Math.max(ans, dfs(i, j));
      }
    }
    return ans;
  }

  private int dfs(int i, int j) {
    if (grid[i][j] == 0) {
      return 0;
    }
    int ans = 1;
    grid[i][j] = 0;
    int[] dirs = {-1, 0, 1, 0, -1};
    for (int k = 0; k < 4; ++k) {
      int x = i + dirs[k], y = j + dirs[k + 1];
      if (x >= 0 && x < m && y >= 0 && y < n) {
        ans += dfs(x, y);
      }
    }
    return ans;
  }
}
class Solution {
public:
  int maxAreaOfIsland(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    int dirs[5] = {-1, 0, 1, 0, -1};
    int ans = 0;
    function<int(int, int)> dfs = [&](int i, int j) {
      if (grid[i][j] == 0) {
        return 0;
      }
      int ans = 1;
      grid[i][j] = 0;
      for (int k = 0; k < 4; ++k) {
        int x = i + dirs[k], y = j + dirs[k + 1];
        if (x >= 0 && x < m && y >= 0 && y < n) {
          ans += dfs(x, y);
        }
      }
      return ans;
    };
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        ans = max(ans, dfs(i, j));
      }
    }
    return ans;
  }
};
func maxAreaOfIsland(grid [][]int) (ans int) {
  m, n := len(grid), len(grid[0])
  dirs := [5]int{-1, 0, 1, 0, -1}
  var dfs func(i, j int) int
  dfs = func(i, j int) int {
    if grid[i][j] == 0 {
      return 0
    }
    ans := 1
    grid[i][j] = 0
    for k := 0; k < 4; k++ {
      x, y := i+dirs[k], j+dirs[k+1]
      if x >= 0 && x < m && y >= 0 && y < n {
        ans += dfs(x, y)
      }
    }
    return ans
  }
  for i := range grid {
    for j := range grid[i] {
      ans = max(ans, dfs(i, j))
    }
  }
  return
}
function maxAreaOfIsland(grid: number[][]): number {
  const m = grid.length;
  const n = grid[0].length;
  const dirs = [-1, 0, 1, 0, -1];
  const dfs = (i: number, j: number): number => {
    if (grid[i][j] === 0) {
      return 0;
    }
    let ans = 1;
    grid[i][j] = 0;
    for (let k = 0; k < 4; ++k) {
      const [x, y] = [i + dirs[k], j + dirs[k + 1]];
      if (x >= 0 && x < m && y >= 0 && y < n) {
        ans += dfs(x, y);
      }
    }
    return ans;
  };
  let ans = 0;
  for (let i = 0; i < m; ++i) {
    for (let j = 0; j < n; ++j) {
      ans = Math.max(ans, dfs(i, j));
    }
  }
  return ans;
}
impl Solution {
  fn dfs(grid: &mut Vec<Vec<i32>>, i: usize, j: usize) -> i32 {
    if i == grid.len() || j == grid[0].len() || grid[i][j] == 0 {
      return 0;
    }
    grid[i][j] = 0;
    let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1);
    if i != 0 {
      res += Self::dfs(grid, i - 1, j);
    }
    if j != 0 {
      res += Self::dfs(grid, i, j - 1);
    }
    res
  }

  pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
    let m = grid.len();
    let n = grid[0].len();
    let mut res = 0;
    for i in 0..m {
      for j in 0..n {
        res = res.max(Self::dfs(&mut grid, i, j));
      }
    }
    res
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文