Skip to content

200. Number of Islands#22

Open
Yuto729 wants to merge 1 commit intomainfrom
number-of-islands
Open

200. Number of Islands#22
Yuto729 wants to merge 1 commit intomainfrom
number-of-islands

Conversation

@Yuto729
Copy link
Owner

@Yuto729 Yuto729 commented Dec 26, 2025

解く問題

Number of Islands

次に解く問題

Max Area of Island

@Yuto729 Yuto729 changed the title Number Of Islands 200. Number of Islands Dec 26, 2025
Repository owner deleted a comment from github-actions bot Dec 26, 2025
上記のコードの問題点
- スタックからpopしたときに`visited`に追加しているので, 同じノードを追加する可能性がある. => TLEの原因
- 例: forループ内で(0, 1)と(1, 0)の両方から(1, 1)を探索候補として追加してしまう.
**visitedへのマークは必ずスタックへの追加とセットで行う.**
Copy link

Choose a reason for hiding this comment

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

必ずセットならば関数にするのは一つですね。

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))
Copy link

Choose a reason for hiding this comment

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

細かい点ですが以下の点が気になりました。

  • 定数はなるべくコード上部にまとめたいなと思いました。
  • 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))

def numIslands(self, grid: List[List[str]]) -> int:
m = len(grid)
n = len(grid[0])
def visite_island(start_x, start_y):
Copy link

Choose a reason for hiding this comment

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

nit: visite_island --> visit_islandでしょうか

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
Copy link

Choose a reason for hiding this comment

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

この条件分岐を一つの関数にしてまとめて

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)]
Copy link

Choose a reason for hiding this comment

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

nit: インデントの深さは揃えておくと読みやすいです.

- 境界チェック
- スタート地点を見つけるときにすでに探索済みの点はスキップする

時間計算量: O(M*N). 空間計算量: O(M*N)(visitedの大きさ)
Copy link

Choose a reason for hiding this comment

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

nit: *がmarkdownの強調表示の囲みとして解釈されていますね.\*とエスケープするとちゃんと表示されます:

時間計算量: O(M*N). 空間計算量: O(M*N)(visitedの大きさ)

@5ky7
Copy link

5ky7 commented Dec 27, 2025

基本的に読みやすかったです.

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:
Copy link

Choose a reason for hiding this comment

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

pythonの場合不等式をつなげられるので、個人的には

not (0 <= next_x < m and 0 <= next_y < n):

がぱっと見で意味が取れるので好みです。

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)]
Copy link

Choose a reason for hiding this comment

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

後半で変えられているようにdirectionsneighborsなどのほうが適切に思います。

continue

queue.append((neighbor_x, neighbor_y))
grid[neighbor_x][neighbor_y] = island_number
Copy link

@maeken4 maeken4 Jan 31, 2026

Choose a reason for hiding this comment

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

grid: List[List[str]]にたいしてintを代入している点が気になりました。

Copy link

@maeken4 maeken4 left a comment

Choose a reason for hiding this comment

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

遅ればせながらいくつかコメントさせて頂きました!

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.

5 participants