-
Notifications
You must be signed in to change notification settings - Fork 0
695. Max Area of Island #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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/) |
| 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) | ||
| 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 個人的には |
||
|
|
||
| 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)$ | ||
| 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)$ |
| 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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秒 | ||
There was a problem hiding this comment.
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では変わってるので、余計なコメントかもしれません。