Skip to content

695. Max Area of Island#37

Open
hiro111208 wants to merge 1 commit into
mainfrom
695-max-area-of-island
Open

695. Max Area of Island#37
hiro111208 wants to merge 1 commit into
mainfrom
695-max-area-of-island

Conversation

@hiro111208

Copy link
Copy Markdown
Owner

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.

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

@hiroki-horiguchi-dev hiroki-horiguchi-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

読みやすいコードだとおもいます!

```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では変わってるので、余計なコメントかもしれません。

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

if not (0 <= r < row_length and 0 <= c < column_length) or (r, c) in visited or grid[r][c] != 1:
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 のほうが自然に感じるためです。趣味の範囲だと思います。

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.

4 participants