Skip to content

200. Number of Islands - #17

Open
kazizi55 wants to merge 1 commit into
mainfrom
200-number-of-islands
Open

200. Number of Islands#17
kazizi55 wants to merge 1 commit into
mainfrom
200-number-of-islands

Conversation

@kazizi55

@kazizi55 kazizi55 commented Aug 5, 2026

Copy link
Copy Markdown
Owner

- 考えたこと
- gemini に聞きながら完成させた。
- M と N の二重ループの中でさらに stack を使って隣接する LAND を全て WATER に破壊的変更をする形で DFS をする。
- イメージで考えると、島の探索隊がいるとして、島の一部を見つけ次第、見つけたという記録をつけて、引き継ぎしながら**その島全体を海に沈める**。そしてまた次の人に引き継ぐ。島が見つからなければどんどん次の人に引き継いでいく。イメージで考えるとなかなかすごいことしてるな、この解法。笑

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

このイメージ面白いですね笑

def numIslands(self, grid: List[List[str]]) -> int:
height = len(grid)
width = len(grid[0])
visited = [[False] * width for _ in range(height)]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはこういう時 set で visited を作ることが多いのですが、ハッシュ値の計算がない分2重配列で持つのも良さそうですね

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。確かにハッシュ値の計算がないのは input がかなり大きくなってきた時などに効いてきそうですね。
ちなみに、次の問題で、visited は list よりも set の方が 座標を保持する lands_to_visit とより対になるように見えて好みと思うようになりました。
https://github.com/kazizi55/coding-challenges/pull/18/changes#diff-63ea722117ed2338f5a398092eca978801bad4637318cf2eb1166a223863a409R310-R312

Comment on lines +267 to +268
WATER = "0"
LAND = "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.

より大掛かりなプログラムになると Enum で持つということも選択肢になるんでしょうか

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
同意です。
また、状態遷移をより明示したい場合などにも Enum が有効だと今は思うようになりました。
#14 (comment)

]
def numIslands(self, grid: List[List[str]]) -> int:
num_islands = 0
rows = 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.

rows という単語からは、行の情報が格納されているリストというニュアンスを感じます。自分なら num_rows と名付けると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
言われてみればおっしゃる通りですね。以降は len であることを変数名に含めようと思います。

continue
num_islands += 1
stack = [(r, c)]
grid[r][c] = self.WATER

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
Hiroto-Iizuka/coding_practice#17 (comment)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

呼び出し側の立場から見ると、渡した値が関数内部で意図せず変更されると驚かれる可能性があります。

練習の一環として入力変更の解法も書いていたのですが、呼び出し側の気持ちを考えると変更する旨を関数名やコメントで明示すべきだったと思い直しました。以降はそのようにしていこうと思います。

num_islands += 1
stack = [(r, c)]
grid[r][c] = self.WATER
while len(stack) > 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
mamo3gr/arai60#6 (comment)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。以降実践していきます。
PEP8 にも同じルールがあるのですね。

For sequences, (strings, lists, tuples), use the fact that empty sequences are false

https://peps.python.org/pep-0008/#:~:text=For%20sequences%2C%20(strings%2C%20lists%2C%20tuples)%2C%20use%20the%20fact%20that%20empty%20sequences%20are%20false%3A

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