695. Max Area of Island#37
Open
hiro111208 wants to merge 1 commit into
Open
Conversation
| 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.
私もコメントもらったのですが、マジックナンバーはローカル変数に切り出した方が良いと思いました。
jjysogfy
reviewed
Jun 1, 2026
| ```python | ||
| class Solution: | ||
| def maxAreaOfIsland(self, grid: List[List[int]]) -> int: | ||
| row_length = len(grid) |
There was a problem hiding this comment.
この変数名はまぎらわしく感じました。各行の長さ、つまりlen(grid[r])のように見えたからです。
step 2では変わってるので、余計なコメントかもしれません。
nodchip
reviewed
Jun 2, 2026
| 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.
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) |
There was a problem hiding this comment.
個人的には + 1 を最後に書きます。理由は b + a * x より a * x + b のほうが自然に感じるためです。趣味の範囲だと思います。
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.
This problem: 695. Max Area of Island
Next problem: 323. Number of Connected Components in an Undirected Graph