diff --git a/695-Max-Area-of-Island/note.md b/695-Max-Area-of-Island/note.md new file mode 100644 index 0000000..022c2ca --- /dev/null +++ b/695-Max-Area-of-Island/note.md @@ -0,0 +1,29 @@ +# 695. Max Area of Island + + + +## step1(まず通す) + +直前に解いた とほぼ同じ。数える対象が違うだけ。 + + では str の "0" or "1", こっちは int の 0 or 1 で海と陸が表現されているが、ゲームのマップとか地図データの処理とかそういうのを考えるといろんな地形をわかりやすい形で表現したいので [enum](https://docs.python.org/ja/3/library/enum.html) で管理したい。 + +(row, column) も、大規模なコードベースなら tuple[int,int] に Position とか名前をつけておきたいかも。 + +## step2(整形&他の人のコードを読む) + +- + - やはり 0/1 だとわかりづらいので、この方は `WATER = 0` のように定数としておいている +- + - area のインクリメントをキューへの追加時にするのか取り出し時にするのか + - 今回の自分のコードは、同じマスを2回キューに詰めないようにキューへ詰めたタイミングで seen への追加を行っており、area のインクリメントも同じところにまとめている + +- + - itertools.product を使うと単純なn重ループのネストを浅くできる + +enum を使う場合、int として比較したいので [IntEnum](https://docs.python.org/ja/3/library/enum.html#enum.IntEnum) を使う。そうすると、作成する maxAreaOfIsland の引数 grid の型も `list[list[NodeType]]` が良さそう。 + +## step3(10分以内にさっとかける * 3回) + +- + - やってる最中に一つ前の問題のレビューをいただいたので反映させた diff --git a/695-Max-Area-of-Island/step1.py b/695-Max-Area-of-Island/step1.py new file mode 100644 index 0000000..19d137c --- /dev/null +++ b/695-Max-Area-of-Island/step1.py @@ -0,0 +1,45 @@ +from collections import deque + + +class Solution: + def maxAreaOfIsland(self, grid: list[list[int]]) -> int: + def get_area_of_island(row: int, column: int) -> int: + nodes_to_see = deque([(row, column)]) + seen_nodes.add((row, column)) + area = 1 + while nodes_to_see: + node = nodes_to_see.popleft() + for r, c in ( + (node[0], node[1] - 1), + (node[0], node[1] + 1), + (node[0] - 1, node[1]), + (node[0] + 1, node[1]), + ): + if not (0 <= r < num_rows and 0 <= c < num_columns): + continue + if (r, c) in seen_nodes: + continue + + if grid[r][c] == 1: + area += 1 + nodes_to_see.append((r, c)) + seen_nodes.add((r, c)) + + return area + + num_rows = len(grid) + num_columns = len(grid[0]) + seen_nodes: set[tuple[int, int]] = set() + max_area_of_island = 0 + for row in range(num_rows): + for column in range(num_columns): + if (row, column) in seen_nodes: + continue + if grid[row][column] == 0: + seen_nodes.add((row, column)) + continue + + area = get_area_of_island(row, column) + max_area_of_island = max(area, max_area_of_island) + + return max_area_of_island diff --git a/695-Max-Area-of-Island/step2.py b/695-Max-Area-of-Island/step2.py new file mode 100644 index 0000000..94b6490 --- /dev/null +++ b/695-Max-Area-of-Island/step2.py @@ -0,0 +1,51 @@ +from collections import deque +from enum import IntEnum +from itertools import product + + +class NodeType(IntEnum): + WATER = 0 + LAND = 1 + + +class Solution: + def maxAreaOfIsland(self, grid: list[list[NodeType]]) -> int: + def get_area_of_island(row: int, column: int) -> int: + nodes_to_see = deque([(row, column)]) + seen_nodes.add((row, column)) + area = 1 + while nodes_to_see: + node = nodes_to_see.popleft() + for r, c in ( + (node[0], node[1] - 1), + (node[0], node[1] + 1), + (node[0] - 1, node[1]), + (node[0] + 1, node[1]), + ): + if not (0 <= r < num_rows and 0 <= c < num_columns): + continue + if (r, c) in seen_nodes: + continue + + if grid[r][c] == NodeType.LAND: + area += 1 + nodes_to_see.append((r, c)) + seen_nodes.add((r, c)) + + return area + + num_rows = len(grid) + num_columns = len(grid[0]) + seen_nodes: set[tuple[int, int]] = set() + max_area_of_island = 0 + for row, column in product(range(num_rows), range(num_columns)): + if (row, column) in seen_nodes: + continue + if grid[row][column] == NodeType.WATER: + seen_nodes.add((row, column)) + continue + + area = get_area_of_island(row, column) + max_area_of_island = max(area, max_area_of_island) + + return max_area_of_island diff --git a/695-Max-Area-of-Island/step3.py b/695-Max-Area-of-Island/step3.py new file mode 100644 index 0000000..d75fca4 --- /dev/null +++ b/695-Max-Area-of-Island/step3.py @@ -0,0 +1,52 @@ +from collections import deque +from enum import IntEnum +from itertools import product + + +class NodeType(IntEnum): + WATER = 0 + LAND = 1 + + +class Solution: + def maxAreaOfIsland(self, grid: list[list[NodeType]]) -> int: + num_rows = len(grid) + num_columns = len(grid[0]) + seen_nodes: set[tuple[int, int]] = set() + + def get_area_of_island(row: int, column: int) -> int: + nodes_to_see = deque([(row, column)]) + seen_nodes.add((row, column)) + area = 1 + while nodes_to_see: + node = nodes_to_see.popleft() + for r, c in ( + (node[0], node[1] - 1), + (node[0], node[1] + 1), + (node[0] - 1, node[1]), + (node[0] + 1, node[1]), + ): + if not (0 <= r < num_rows and 0 <= c < num_columns): + continue + if (r, c) in seen_nodes: + continue + + if grid[r][c] == NodeType.LAND: + area += 1 + nodes_to_see.append((r, c)) + seen_nodes.add((r, c)) + + return area + + max_area_of_island = 0 + for row, column in product(range(num_rows), range(num_columns)): + if (row, column) in seen_nodes: + continue + if grid[row][column] == NodeType.WATER: + seen_nodes.add((row, column)) + continue + + area = get_area_of_island(row, column) + max_area_of_island = max(area, max_area_of_island) + + return max_area_of_island