Skip to content

695. Max Area of Island#18

Open
rimokem wants to merge 1 commit into
mainfrom
0695-max-area-of-island
Open

695. Max Area of Island#18
rimokem wants to merge 1 commit into
mainfrom
0695-max-area-of-island

Conversation

@rimokem
Copy link
Copy Markdown
Owner

@rimokem rimokem commented Apr 12, 2026

Comment thread 0695/memo.md
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)]
Copy link
Copy Markdown

@atmaxstar atmaxstar Apr 12, 2026

Choose a reason for hiding this comment

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

ここで急にstackと出て戸惑ってしまったので命名をto_visit(これから訪れる座標)とかにすると分かりやすいかもです。

Comment thread 0695/step3.py
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
Copy link
Copy Markdown

@olsen-blue olsen-blue Apr 12, 2026

Choose a reason for hiding this comment

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

全体的に綺麗で読みやすく(命名、早期continue、改行の分離など)良いのではないかと思いました。

細かい点の好みですが、下記2点感想を共有いたします。
LAND = 1, WATER = 0などとすると、マジックナンバーでなくなるのでより良いかもしれません。これに加えて、早期continueの条件式も、if grid[row][col] != LAND:としてみたりすると、より題意に沿うという意味で、ベターなのではと思います。
・calculate_island_areaは再帰関数で書くのもおすすめです(親と同じような出力を子分に期待できるというのは強力です)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants