zoukankan      html  css  js  c++  java
  • leetcode 695. Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, 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.

    Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

    Example 1:

    [[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]]
    

    Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

    Example 2:

    [[0,0,0,0,0,0,0,0]]

    Given the above grid, return 0.

    Note: The length of each dimension in the given grid does not exceed 50.

    解法1:dfs

    class Solution(object):
        def maxAreaOfIsland(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            # reset 1 to 0 use dfs, and return the ones numer
            row = len(grid)
            col = len(grid[0])
            ans = 0
            
            def dfs(grid, i, j):            
                if i==row or j==col or i<0 or j<0 or grid[i][j]==0: return 0               
                res = 1
                grid[i][j] = 0
                res += dfs(grid, i+1, j)
                res += dfs(grid, i-1, j)
                res += dfs(grid, i, j-1)
                res += dfs(grid, i, j+1)
                return res
            
            for i in range(0, row):
                for j in range(0, col):
                    if grid[i][j] == 1:
                        ans = max(ans, dfs(grid, i, j))
            return ans
            

    解法2:BFS,必须在if里将1reset为0,否则有重复:

    class Solution(object):
        def maxAreaOfIsland(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            # reset 1 to 0 use bfs, and return the ones numer
            row = len(grid)
            col = len(grid[0])
            ans = 0
            
            def bfs(grid, i, j):            
                assert grid[i][j] == 1
                q = [(i,j)]
                grid[i][j]=0
                res = 1
                while q:                
                    q2 = []
                    for i,j in q:                    
                        if i+1<row and grid[i+1][j]==1:
                            grid[i+1][j]=0
                            q2.append((i+1,j))
                            res += 1
                        if i-1>=0 and grid[i-1][j]==1: 
                            grid[i-1][j]=0
                            q2.append((i-1,j))                        
                            res += 1
                        if j+1<col and grid[i][j+1]==1: 
                            grid[i][j+1]=0
                            q2.append((i,j+1))                        
                            res += 1
                        if j-1>=0 and grid[i][j-1]==1: 
                            grid[i][j-1]=0
                            q2.append((i,j-1))                           
                            res += 1
                    q = q2
                return res
            
            for i in range(0, row):
                for j in range(0, col):
                    if grid[i][j] == 1:
                        ans = max(ans, bfs(grid, i, j))
            return ans
            
  • 相关阅读:
    学习资料(干货汇集)
    Android安全系列之:如何在native层保存关键信息
    IntelliJ IDEA 2019 快捷键终极大全,速度收藏!
    【转】45个实用的JavaScript技巧、窍门和最佳实践
    Android中jsoup的混淆规则【转】
    Android WebServer相关项目
    【转】实战nanoHTTPD嵌入android app(3)
    【.net 深呼吸】程序集的热更新
    【WCF】使用“用户名/密码”验证的合理方法
    【Win 10 应用开发】应用预启动
  • 原文地址:https://www.cnblogs.com/bonelee/p/8586921.html
Copyright © 2011-2022 走看看