-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMaxAreaOfIsland.py
61 lines (47 loc) · 1.96 KB
/
MaxAreaOfIsland.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
# @File : MaxAreaOfIsland.py
# @Date : 2020-03-07
# @Author : tc
"""
题号 695. 岛屿的最大面积
给定一个包含了一些 0 和 1的非空二维数组 grid,一个岛屿是由四个方向 (水平或垂直)的1 (代表土地)构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。
找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)
示例 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]]
对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。
示例 2:
[[0,0,0,0,0,0,0,0]]
对于上面这个给定的矩阵, 返回 0。
注意: 给定的矩阵grid 的长度和宽度都不超过 50。
和第200题岛屿数量类似
参考:https://leetcode.com/problems/max-area-of-island/discuss/108541/easy-python
"""
from typing import List
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
m,n = len(grid),len(grid[0])
def dfs(i,j):
if 0 <= i < m and 0 <= j < n and grid[i][j]:
grid[i][j] = 0
return 1 + dfs(i-1,j) + dfs(i+1,j) + dfs(i,j+1) + dfs(i,j-1)
return 0
areas = [dfs(i,j) for i in range(m) for j in range(n) if grid[i][j]]
return max(areas) if areas else 0
if __name__ == '__main__':
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]]
solution = Solution()
print(solution.maxAreaOfIsland(grid))