Open
Conversation
oda
reviewed
Dec 26, 2025
| 上記のコードの問題点 | ||
| - スタックからpopしたときに`visited`に追加しているので, 同じノードを追加する可能性がある. => TLEの原因 | ||
| - 例: forループ内で(0, 1)と(1, 0)の両方から(1, 1)を探索候補として追加してしまう. | ||
| **visitedへのマークは必ずスタックへの追加とセットで行う.** |
t9a-dev
reviewed
Dec 27, 2025
Comment on lines
+264
to
+287
| m = len(grid) | ||
| n = len(grid[0]) | ||
| LAND = "1" | ||
| WATER = "0" | ||
|
|
||
| def traverse_island(x, y): | ||
| stack = [(x, y)] | ||
| directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] | ||
| while stack: | ||
| x, y = stack.pop() | ||
| for direction in directions: | ||
| neighbor_x = x + direction[0] | ||
| neighbor_y = y + direction[1] | ||
| if not (0 <= neighbor_x < m and 0 <= neighbor_y < n): | ||
| continue | ||
|
|
||
| if (neighbor_x, neighbor_y) in visited: | ||
| continue | ||
|
|
||
| if grid[neighbor_x][neighbor_y] == WATER: | ||
| continue | ||
|
|
||
| stack.append((neighbor_x, neighbor_y)) | ||
| visited.add((neighbor_x, neighbor_y)) |
There was a problem hiding this comment.
細かい点ですが以下の点が気になりました。
- 定数はなるべくコード上部にまとめたいなと思いました。
- traverse_island内のdirectionsも定数にしても良いかなと思いました。
- direction[0],directiion[1] はループでタプルで取り出して、変数名で意味をもたせると少し読みやすくなると思いました。
Suggested change
| m = len(grid) | |
| n = len(grid[0]) | |
| LAND = "1" | |
| WATER = "0" | |
| def traverse_island(x, y): | |
| stack = [(x, y)] | |
| directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] | |
| while stack: | |
| x, y = stack.pop() | |
| for direction in directions: | |
| neighbor_x = x + direction[0] | |
| neighbor_y = y + direction[1] | |
| if not (0 <= neighbor_x < m and 0 <= neighbor_y < n): | |
| continue | |
| if (neighbor_x, neighbor_y) in visited: | |
| continue | |
| if grid[neighbor_x][neighbor_y] == WATER: | |
| continue | |
| stack.append((neighbor_x, neighbor_y)) | |
| visited.add((neighbor_x, neighbor_y)) | |
| LAND = "1" | |
| WATER = "0" | |
| DIRECTIONS = ((0, 1), (1, 0), (0, -1), (-1, 0)) | |
| m = len(grid) | |
| n = len(grid[0]) | |
| def traverse_island(x, y): | |
| stack = [(x, y)] | |
| while stack: | |
| x, y = stack.pop() | |
| for dx, dy in DIRECTIONS: | |
| neighbor_x = x + dx | |
| neighbor_y = y + dy | |
| if not (0 <= neighbor_x < m and 0 <= neighbor_y < n): | |
| continue | |
| if (neighbor_x, neighbor_y) in visited: | |
| continue | |
| if grid[neighbor_x][neighbor_y] == WATER: | |
| continue | |
| stack.append((neighbor_x, neighbor_y)) | |
| visited.add((neighbor_x, neighbor_y)) |
5ky7
reviewed
Dec 27, 2025
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| m = len(grid) | ||
| n = len(grid[0]) | ||
| def visite_island(start_x, start_y): |
Comment on lines
+24
to
+31
| if next_x < 0 or next_x >= m or next_y < 0 or next_y >= n: | ||
| continue | ||
|
|
||
| if (next_x, next_y) in visited: | ||
| continue | ||
|
|
||
| if grid[next_x][next_y] == "0": | ||
| continue |
There was a problem hiding this comment.
この条件分岐を一つの関数にしてまとめて
if not is_valid_grid(next_x, next_y):
continue;のようにするか,後述されていますが島や海を示す値に名前をつけてしまうのが好みです(もっとも今回はコード全体の長さ的に今のままでも糸はスムーズに伝わるとは思いますが).
Comment on lines
+55
to
+56
| def visite_island(start_x, start_y): | ||
| stack = [(start_x, start_y)] |
| - 境界チェック | ||
| - スタート地点を見つけるときにすでに探索済みの点はスキップする | ||
|
|
||
| 時間計算量: O(M*N). 空間計算量: O(M*N)(visitedの大きさ) |
There was a problem hiding this comment.
nit: *がmarkdownの強調表示の囲みとして解釈されていますね.\*とエスケープするとちゃんと表示されます:
時間計算量: O(M*N). 空間計算量: O(M*N)(visitedの大きさ)
|
基本的に読みやすかったです. |
maeken4
reviewed
Jan 31, 2026
| for path in path_list: | ||
| next_x = x + path[0] | ||
| next_y = y + path[1] | ||
| if next_x < 0 or next_x >= m or next_y < 0 or next_y >= n: |
There was a problem hiding this comment.
pythonの場合不等式をつなげられるので、個人的には
not (0 <= next_x < m and 0 <= next_y < n):がぱっと見で意味が取れるので好みです。
maeken4
reviewed
Jan 31, 2026
| n = len(grid[0]) | ||
| def visite_island(start_x, start_y): | ||
| stack = [(start_x, start_y)] | ||
| path_list = [(0, 1), (1, 0), (0, -1), (-1, 0)] |
There was a problem hiding this comment.
後半で変えられているようにdirectionsやneighborsなどのほうが適切に思います。
maeken4
reviewed
Jan 31, 2026
| continue | ||
|
|
||
| queue.append((neighbor_x, neighbor_y)) | ||
| grid[neighbor_x][neighbor_y] = island_number |
There was a problem hiding this comment.
grid: List[List[str]]にたいしてintを代入している点が気になりました。
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.
解く問題
Number of Islands
次に解く問題
Max Area of Island