Skip to content

200.number of islands#17

Open
nicah4o wants to merge 2 commits into
mainfrom
200.number-of-islands
Open

200.number of islands#17
nicah4o wants to merge 2 commits into
mainfrom
200.number-of-islands

Conversation

@nicah4o
Copy link
Copy Markdown
Owner

@nicah4o nicah4o commented May 19, 2026

};
```

こちらは空間計算量もO(NM)になる。DFSはO(1)になるから問題の性質上走査するので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.

DFSとBFSで空間計算量は変わりますか?

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.

レビューありがとうございます。
DFSの方が空間計算量は節約できると思っていましたがすべてが島の場合を考えると再帰がNM回回るのでその分のメモリが保存され違いますね。
空間計算量はどちらも変わらずO(NM)でした。

while (!islands.empty()) {
auto [row, column] = islands.front();
islands.pop();
for (int i = 0; i < 4; i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

i < 4ではなく、i < directions.size()が良いと思います。

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.

そうですね、マジックナンバーになってます。

参考:https://github.com/attractal/leetcode/pull/8/changes

方法としては
 1.m行n列のbool配列visitedを作る。falseで初期化して上陸したらtrueにする。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一応ですが、visitedは幅優先探索のためではなく、入力を破壊しない方法の一つとして導入されていますね。

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.

なるほど、確かに破壊ありならvisitedを使わずに島を沈没させる方向でも書けますね。
役割分担に気づいていませんでした。ありがとうございます。

参考:https://leetcode.com/problems/number-of-islands/description/comments/1570785/

やり方としては
 1.visit関数をラムダ式でnumIslands関数の中に書く
Copy link
Copy Markdown

@h-masder h-masder May 19, 2026

Choose a reason for hiding this comment

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

自分でも調べてみようと思うのですが、
private関数を使わずにラムダ式を使う利点はあったりしますか?

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.

visit関数でgridを直接処理に使いたいのですがそれをprivateに書くと引数が一つ増えるので関数内でラムダ式を使用しました。

public:
int numIslands(vector<vector<char>>& grid) {
auto visit = [&](auto& self, int x, int y) -> void {
if (0 <= x && x < grid[0].size() && 0 <= y && y < grid.size() &&
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

条件式が長すぎるように感じるので、別のif文に分けるなどしてもいいですね

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.

レビューありがとうございます。
'1'の判定を分けるのがよさそうです。

Comment on lines +67 to +68
int rows = grid.size();
int columns = grid[0].size();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num_rows, num_cols とすると、より行「数」という意味が伝わりやすいと思います

}
}
};
for (int m = 0; m < grid.size(); m++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

「行」を表す変数で、row, x, m などの異なる名前が付いているので、統一した方がいいかなと思いました

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.

ありがとうございます。
確かにrow,columnで統一したほうがいいですね。

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