Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 695_max_area_of_island/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## 問題: [695. Max Area of Island](https://leetcode.com/problems/max-area-of-island/description/)
33 changes: 33 additions & 0 deletions 695_max_area_of_island/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Step 1

- 各セルで1かつ訪れていなかったらdfsを行う
- dfs
- セルがgridの外、すでに訪れている、もしくは1でない場合は0を返す
- セルが1だったら、隣接するセルのdfsの戻り値に1を足した値を返す

```python
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
row_length = len(grid)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この変数名はまぎらわしく感じました。各行の長さ、つまりlen(grid[r])のように見えたからです。
step 2では変わってるので、余計なコメントかもしれません。

column_length = len(grid[0])
visited = set()
max_area = 0

def dfs(r, c):
if not (0 <= r < row_length and 0 <= c < column_length) or (r, c) in visited or grid[r][c] != 1:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 行に多くの条件を詰め込み過ぎており、読みにくく感じました。適宜分割したほうが読みやすくなると思います。

if not (0 <= r < row_length and 0 <= c < column_length):
    return 0

if (r, c) in visited:
    return 0

if grid[r][c] != 1:
    return 0

return 0
visited.add((r, c))
return 1 + dfs(r + 1, c) + dfs(r - 1, c) + dfs(r, c + 1) + dfs(r, c - 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には + 1 を最後に書きます。理由は b + a * x より a * x + b のほうが自然に感じるためです。趣味の範囲だと思います。


for r in range(row_length):
for c in range(column_length):
max_area = max(max_area, dfs(r, c))

return max_area
```

時間計算量: $O(mn)$

空間計算量: $O(mn)$

アルゴリズム最大実行時間(概算): 時間計算量 / Pythonの1秒あたりの計算ステップ数 -> $(50 \times 50) \div 10,000,000 = 0.000025 (s)$
30 changes: 30 additions & 0 deletions 695_max_area_of_island/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Step 2

```python
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
num_rows = len(grid)
num_columns = len(grid[0])
visited = set()
max_area = 0

def measure_island(r, c):
if not (0 <= r < num_rows and 0 <= c < num_columns) or (r, c) in visited or grid[r][c] != 1:
return 0
visited.add((r, c))
return 1 + measure_island(r + 1, c) + measure_island(r - 1, c) + measure_island(r, c + 1) + measure_island(r, c - 1)

for r in range(num_rows):
for c in range(num_columns):
if grid[r][c] == 1 and (r, c) not in visited:
max_area = max(max_area, measure_island(r, c))

return max_area

```

時間計算量: $O(mn)$

空間計算量: $O(mn)$

アルゴリズム最大実行時間(概算): 時間計算量 / Pythonの1秒あたりの計算ステップ数 -> $(50 \times 50) \div 10,000,000 = 0.000025 (s)$
29 changes: 29 additions & 0 deletions 695_max_area_of_island/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Step 3

```python
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
num_rows = len(grid)
num_columns = len(grid[0])
visited = set()
max_area = 0

def measure_island(r, c):
if not (0 <= r < num_rows and 0 <= c < num_columns) or (r, c) in visited or grid[r][c] != 1:

@hiroki-horiguchi-dev hiroki-horiguchi-dev Jun 1, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私もコメントもらったのですが、マジックナンバーはローカル変数に切り出した方が良いと思いました。

return 0
visited.add((r, c))
return 1 + measure_island(r + 1, c) + measure_island(r - 1, c) + measure_island(r, c + 1) + measure_island(r, c - 1)

for r in range(num_rows):
for c in range(num_columns):
if grid[r][c] == 1 and (r, c) not in visited:
max_area = max(max_area, measure_island(r, c))

return max_area
```

1回目: 4分 0秒

2回目: 3分 41秒

3回目: 3分 9秒