695. Max Area of Island#18
Open
rimokem wants to merge 1 commit into
Open
Conversation
atmaxstar
reviewed
Apr 12, 2026
| visited = [[False] * num_cols for _ in range(num_rows)] | ||
|
|
||
| def calculate_island_area(start_row: int, start_col: int) -> int: | ||
| stack = [(start_row, start_col)] |
There was a problem hiding this comment.
ここで急にstackと出て戸惑ってしまったので命名をto_visit(これから訪れる座標)とかにすると分かりやすいかもです。
olsen-blue
reviewed
Apr 12, 2026
Comment on lines
+4
to
+33
| class Solution: | ||
| def maxAreaOfIsland(self, grid: List[List[int]]) -> int: | ||
| num_rows = len(grid) | ||
| num_cols = len(grid[0]) | ||
| visited = [[False] * num_cols for _ in range(num_rows)] | ||
|
|
||
| def calculate_island_area(start_row: int, start_col: int) -> int: | ||
| stack = [(start_row, start_col)] | ||
| area = 0 | ||
| while stack: | ||
| row, col = stack.pop() | ||
| if not (0 <= row < num_rows and 0 <= col < num_cols): | ||
| continue | ||
| if grid[row][col] == 0 or visited[row][col]: | ||
| continue | ||
| visited[row][col] = True | ||
| area += 1 | ||
| stack.extend( | ||
| [(row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1)] | ||
| ) | ||
| return area | ||
|
|
||
| max_area = 0 | ||
| for row in range(num_rows): | ||
| for col in range(num_cols): | ||
| if grid[row][col] == 0 or visited[row][col]: | ||
| continue | ||
| area = calculate_island_area(row, col) | ||
| max_area = max(max_area, area) | ||
| return max_area |
There was a problem hiding this comment.
全体的に綺麗で読みやすく(命名、早期continue、改行の分離など)良いのではないかと思いました。
細かい点の好みですが、下記2点感想を共有いたします。
・LAND = 1, WATER = 0などとすると、マジックナンバーでなくなるのでより良いかもしれません。これに加えて、早期continueの条件式も、if grid[row][col] != LAND:としてみたりすると、より題意に沿うという意味で、ベターなのではと思います。
・calculate_island_areaは再帰関数で書くのもおすすめです(親と同じような出力を子分に期待できるというのは強力です)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
今回の問題
https://leetcode.com/problems/max-area-of-island/
次回の問題
https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/