|
| 1 | +class Solution: |
| 2 | + ISLAND_MARK = 1 |
| 3 | + |
| 4 | + def _is_in_boundary(self, row_index: int, column_index: int) -> bool: |
| 5 | + if (row_index < self._row_num and row_index >= 0) and \ |
| 6 | + (column_index < self._column_num and column_index >= 0): |
| 7 | + return True |
| 8 | + |
| 9 | + return False |
| 10 | + |
| 11 | + def _get_island_size(self, |
| 12 | + grid: List[List[int]], |
| 13 | + visited: List[List[int]], |
| 14 | + group_id: int, |
| 15 | + row_index: int, |
| 16 | + column_index: int) -> int: |
| 17 | + # [ up, down, left, right] |
| 18 | + four_directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] |
| 19 | + |
| 20 | + queue = [] |
| 21 | + island_size = 1 |
| 22 | + |
| 23 | + queue.append((row_index, column_index)) |
| 24 | + |
| 25 | + while queue: |
| 26 | + row_index, column_index = queue.pop(0) |
| 27 | + visited[row_index][column_index] = group_id |
| 28 | + |
| 29 | + for direction in four_directions: |
| 30 | + next_row_index = row_index + direction[1] |
| 31 | + next_col_index = column_index + direction[0] |
| 32 | + |
| 33 | + if self._is_in_boundary(next_row_index, next_col_index): |
| 34 | + is_part_island = grid[next_row_index][next_col_index] == Solution.ISLAND_MARK |
| 35 | + is_visited = visited[next_row_index][next_col_index] != -1 |
| 36 | + |
| 37 | + if is_part_island and not is_visited: |
| 38 | + island_size += 1 |
| 39 | + visited[next_row_index][next_col_index] = group_id |
| 40 | + queue.append((next_row_index, next_col_index)) |
| 41 | + |
| 42 | + return island_size |
| 43 | + |
| 44 | + def _get_max_island_size(self, |
| 45 | + grid: List[List[int]], |
| 46 | + visited: List[List[int]], |
| 47 | + group_id_island_count_map: Dict[int, int], |
| 48 | + row_index: int, |
| 49 | + column_index: int) -> int: |
| 50 | + # [ up, down, left, right] |
| 51 | + four_directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] |
| 52 | + |
| 53 | + island_size = 1 |
| 54 | + group_ids = [] |
| 55 | + for direction in four_directions: |
| 56 | + next_row_index = row_index + direction[1] |
| 57 | + next_col_index = column_index + direction[0] |
| 58 | + |
| 59 | + if self._is_in_boundary(next_row_index, next_col_index): |
| 60 | + is_part_island = grid[next_row_index][next_col_index] == Solution.ISLAND_MARK |
| 61 | + |
| 62 | + if is_part_island: |
| 63 | + group_id = visited[next_row_index][next_col_index] |
| 64 | + group_ids.append(group_id) |
| 65 | + |
| 66 | + # using set() to make sure, not to add same group id more than once |
| 67 | + for group_id in set(group_ids): |
| 68 | + island_size += group_id_island_count_map[group_id] |
| 69 | + |
| 70 | + return island_size |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + def largestIsland(self, grid: List[List[int]]) -> int: |
| 75 | + self._row_num = len(grid) |
| 76 | + self._column_num = len(grid[0]) |
| 77 | + |
| 78 | + visited = [[-1]*self._column_num for _ in range(self._row_num)] |
| 79 | + |
| 80 | + group_id = 1 |
| 81 | + group_id_island_count_map = {} |
| 82 | + # will be runnning BFS to explore islands. |
| 83 | + for row_index in range(self._row_num): |
| 84 | + for column_index in range(self._column_num): |
| 85 | + if grid[row_index][column_index] == 1 and visited[row_index][column_index] == -1: |
| 86 | + island_size = self._get_island_size(grid, visited, group_id, row_index, column_index) |
| 87 | + group_id_island_count_map[group_id] = island_size |
| 88 | + group_id += 1 |
| 89 | + |
| 90 | + max_island_size = -1 |
| 91 | + # now searching for max island size |
| 92 | + for row_index in range(self._row_num): |
| 93 | + for column_index in range(self._column_num): |
| 94 | + if grid[row_index][column_index] == 0: |
| 95 | + max_island_size = max(max_island_size, self._get_max_island_size(grid, visited, group_id_island_count_map, row_index, column_index)) |
| 96 | + |
| 97 | + if max_island_size == -1: |
| 98 | + # it means there is no zero in the grid, so the whole grid is the island |
| 99 | + max_island_size = self._row_num * self._column_num |
| 100 | + |
| 101 | + return max_island_size |
0 commit comments