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
        

 

转载于:https://www.cnblogs.com/bonelee/p/8586921.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ogging to D:\Program Files (x86)\Fate/hollow ataraxia\FateFD.exe.console.log started on Friday, October 07, 2011 12:48:08 12:48:07 ! (info) Loading options from embedded area... 12:48:07 ! 吉里吉里[きりきり] 2 実行コア/2.25.11.909 (SVN revision:1109; Compiled on Sep 12 2005 22:44:16) TJS2/2.4.19 Copyright (C) 1997-2005 W.Dee and contributors All rights reserved. 12:48:07 ! バージョン情報の詳細は Ctrl + F12 で閲覧できます 12:48:07 ! Program started on Windows NT 6.1.3505 Service Pack 1 (Win32) 12:48:07 ! (info) Total physical memory : -1 12:48:07 ! (info) Selected project directory : file://./d/program files (x86)/fate/hollow ataraxia/data.xp3> 12:48:07 ! (info) CPU #0 : FPU:yes MMX:yes 3DN:no SSE:yes CMOVcc:yes E3DN:no EMMX:yes SSE2:yes TSC:yes Intel(GenuineIntel) [Intel(R) Core(TM) i5 CPU M 430 @ 2.27GHz] CPUID(1)/EAX=00020652 CPUID(1)/EBX=00100800 12:48:07 ! (info) CPU #1 : FPU:yes MMX:yes 3DN:no SSE:yes CMOVcc:yes E3DN:no EMMX:yes SSE2:yes TSC:yes Intel(GenuineIntel) [Intel(R) Core(TM) i5 CPU M 430 @ 2.27GHz] CPUID(1)/EAX=00020652 CPUID(1)/EBX=01100800 12:48:07 ! (info) CPU #2 : FPU:yes MMX:yes 3DN:no SSE:yes CMOVcc:yes E3DN:no EMMX:yes SSE2:yes TSC:yes Intel(GenuineIntel) [Intel(R) Core(TM) i5 CPU M 430 @ 2.27GHz] CPUID(1)/EAX=00020652 CPUID(1)/EBX=04100800 12:48:07 ! (info) CPU #3 : FPU:yes MMX:yes 3DN:no SSE:yes CMOVcc:yes E3DN:no EMMX:yes SSE2:yes TSC:yes Intel(GenuineIntel) [Intel(R) Core(TM) i5 CPU M 430 @ 2.27GHz] CPUID(1)/EAX=00020652 CPUID(1)/EBX=05100800 12:48:07 ! (info) finally detected CPU features : FPU:yes MMX:yes 3DN:no SSE:yes CMOVcc:yes E3DN:no EMMX:yes SSE2:yes TSC:yes 12:48:07 ! (info) Specified option(s) : -debugwin=no 12:48:07 ! (info) Loading cxdec.tpm 12:48:08 ! OS : Windows NT 6.1.3505 Service Pack 1 (Win32) 12:48:08 ! KAG : 3.25 beta 10 TYPE-MOON customized 12:48:08 ! Kirikiri : 2.25.11.909
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值