From c9a0de527e23a372a08ce2bdd0d6f761ac249477 Mon Sep 17 00:00:00 2001 From: mayota Date: Sun, 28 Jun 2026 19:29:19 +0900 Subject: [PATCH 1/2] step1,2,3 --- 695-Max-Area-of-Island/note.md | 26 +++++++++++++++++ 695-Max-Area-of-Island/step1.py | 45 +++++++++++++++++++++++++++++ 695-Max-Area-of-Island/step2.py | 51 +++++++++++++++++++++++++++++++++ 695-Max-Area-of-Island/step3.py | 51 +++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 695-Max-Area-of-Island/note.md create mode 100644 695-Max-Area-of-Island/step1.py create mode 100644 695-Max-Area-of-Island/step2.py create mode 100644 695-Max-Area-of-Island/step3.py diff --git a/695-Max-Area-of-Island/note.md b/695-Max-Area-of-Island/note.md new file mode 100644 index 0000000..2c6b388 --- /dev/null +++ b/695-Max-Area-of-Island/note.md @@ -0,0 +1,26 @@ +# 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..94b6490 --- /dev/null +++ b/695-Max-Area-of-Island/step3.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 From 9883c7cd4d943bef87a724f8809a8c99c0e99116 Mon Sep 17 00:00:00 2001 From: mayota Date: Sun, 28 Jun 2026 19:32:17 +0900 Subject: [PATCH 2/2] step3 --- 695-Max-Area-of-Island/note.md | 3 +++ 695-Max-Area-of-Island/step3.py | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/695-Max-Area-of-Island/note.md b/695-Max-Area-of-Island/note.md index 2c6b388..022c2ca 100644 --- a/695-Max-Area-of-Island/note.md +++ b/695-Max-Area-of-Island/note.md @@ -24,3 +24,6 @@ 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/step3.py b/695-Max-Area-of-Island/step3.py index 94b6490..d75fca4 100644 --- a/695-Max-Area-of-Island/step3.py +++ b/695-Max-Area-of-Island/step3.py @@ -10,6 +10,10 @@ class NodeType(IntEnum): 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)) @@ -34,9 +38,6 @@ def get_area_of_island(row: int, column: int) -> int: 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: