695. Max Area of Island

本文介绍了一种使用深度优先搜索(DFS)算法来寻找二维二进制矩阵中最大岛屿面积的方法。通过对每个单元格进行递归搜索并标记已访问过的陆地,确保了岛屿面积计算的准确性。文章通过具体示例展示了算法的应用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

695. Max Area of Island

Medium

6048149Add to ListShare

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.

 

class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
        """
        assert Solution().maxAreaOfIsland([[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]]) == 6
        assert Solution().maxAreaOfIsland([[0, 0, 0, 0, 0, 0, 0, 0]]) == 0
        assert Solution().maxAreaOfIsland([[0, 0, 0, 0, 0, 0, 0, 1]]) == 1
        assert Solution().maxAreaOfIsland([[1, 1, 0, 0, 0, 0, 0, 1]]) == 2
        assert Solution().maxAreaOfIsland([[1]]) == 1
        
        解题思路:深搜四个方向遍历标记
        时间复杂度:O(nm) 空间复杂度:O(1)
        """
        maxArea, n, m = 0, len(grid), len(grid[0])
        d = [[0, 1], [1, 0], [-1, 0], [0, -1]]

        # 深搜遍历
        def dfs(x: int, y: int) -> int:
            if grid[x][y] == 0:
                return 0

            grid[x][y] = 0
            area = 1
            for i in range(len(d)):
                nextX = x + d[i][0]
                nextY = y + d[i][1]
                if nextX >= n or nextY >= m or nextX < 0 or nextY < 0:
                    continue
                area += dfs(nextX, nextY)
            return area

        for i in range(n):
            for j in range(m):
                maxArea = max(maxArea, dfs(i, j))

        return maxArea

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值