|
| 1 | +""" |
| 2 | +Given an two dimensional binary matrix grid. An island is a group of 1's (representing |
| 3 | +land) connected 4-directionally (horizontal or vertical.) You may assume all four edges |
| 4 | +of the grid are surrounded by water. The area of an island is the number of cells with |
| 5 | +a value 1 in the island. Return the maximum area of an island in a grid. If there is no |
| 6 | +island, return 0. |
| 7 | +""" |
| 8 | + |
| 9 | +matrix = [ |
| 10 | + [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 11 | + [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], |
| 12 | + [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], |
| 13 | + [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0], |
| 14 | + [0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0], |
| 15 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], |
| 16 | + [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], |
| 17 | + [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +def is_safe(row: int, col: int, rows: int, cols: int) -> bool: |
| 22 | + """ |
| 23 | + Checking whether coordinate (row, col) is valid or not. |
| 24 | +
|
| 25 | + >>> is_safe(0, 0, 5, 5) |
| 26 | + True |
| 27 | + >>> is_safe(-1,-1, 5, 5) |
| 28 | + False |
| 29 | + """ |
| 30 | + return 0 <= row < rows and 0 <= col < cols |
| 31 | + |
| 32 | + |
| 33 | +def depth_first_search(row: int, col: int, seen: set, mat: list[list[int]]) -> int: |
| 34 | + """ |
| 35 | + Returns the current area of the island |
| 36 | +
|
| 37 | + >>> depth_first_search(0, 0, set(), matrix) |
| 38 | + 0 |
| 39 | + """ |
| 40 | + rows = len(mat) |
| 41 | + cols = len(mat[0]) |
| 42 | + if is_safe(row, col, rows, cols) and (row, col) not in seen and mat[row][col] == 1: |
| 43 | + seen.add((row, col)) |
| 44 | + return ( |
| 45 | + 1 |
| 46 | + + depth_first_search(row + 1, col, seen, mat) |
| 47 | + + depth_first_search(row - 1, col, seen, mat) |
| 48 | + + depth_first_search(row, col + 1, seen, mat) |
| 49 | + + depth_first_search(row, col - 1, seen, mat) |
| 50 | + ) |
| 51 | + else: |
| 52 | + return 0 |
| 53 | + |
| 54 | + |
| 55 | +def find_max_area(mat: list[list[int]]) -> int: |
| 56 | + """ |
| 57 | + Finds the area of all islands and returns the maximum area. |
| 58 | +
|
| 59 | + >>> find_max_area(matrix) |
| 60 | + 6 |
| 61 | + """ |
| 62 | + seen: set = set() |
| 63 | + |
| 64 | + max_area = 0 |
| 65 | + for row, line in enumerate(mat): |
| 66 | + for col, item in enumerate(line): |
| 67 | + if item == 1 and (row, col) not in seen: |
| 68 | + # Maximizing the area |
| 69 | + max_area = max(max_area, depth_first_search(row, col, seen, mat)) |
| 70 | + return max_area |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + import doctest |
| 75 | + |
| 76 | + print(find_max_area(matrix)) # Output -> 6 |
| 77 | + |
| 78 | + """ |
| 79 | + Explanation: |
| 80 | + We are allowed to move in four directions (horizontal or vertical) so the possible |
| 81 | + in a matrix if we are at x and y position the possible moving are |
| 82 | +
|
| 83 | + Directions are [(x, y+1), (x, y-1), (x+1, y), (x-1, y)] but we need to take care of |
| 84 | + boundary cases as well which are x and y can not be smaller than 0 and greater than |
| 85 | + the number of rows and columns respectively. |
| 86 | +
|
| 87 | + Visualization |
| 88 | + mat = [ |
| 89 | + [0,0,A,0,0,0,0,B,0,0,0,0,0], |
| 90 | + [0,0,0,0,0,0,0,B,B,B,0,0,0], |
| 91 | + [0,C,C,0,D,0,0,0,0,0,0,0,0], |
| 92 | + [0,C,0,0,D,D,0,0,E,0,E,0,0], |
| 93 | + [0,C,0,0,D,D,0,0,E,E,E,0,0], |
| 94 | + [0,0,0,0,0,0,0,0,0,0,E,0,0], |
| 95 | + [0,0,0,0,0,0,0,F,F,F,0,0,0], |
| 96 | + [0,0,0,0,0,0,0,F,F,0,0,0,0] |
| 97 | + ] |
| 98 | +
|
| 99 | + For visualization, I have defined the connected island with letters |
| 100 | + by observation, we can see that |
| 101 | + A island is of area 1 |
| 102 | + B island is of area 4 |
| 103 | + C island is of area 4 |
| 104 | + D island is of area 5 |
| 105 | + E island is of area 6 and |
| 106 | + F island is of area 5 |
| 107 | +
|
| 108 | + it has 6 unique islands of mentioned areas |
| 109 | + and the maximum of all of them is 6 so we return 6. |
| 110 | + """ |
| 111 | + |
| 112 | + doctest.testmod() |
0 commit comments