Skip to content

Set Matrix Zeroes#154

Open
tom4649 wants to merge 2 commits into
mainfrom
73.Set-Matrix-Zeroes
Open

Set Matrix Zeroes#154
tom4649 wants to merge 2 commits into
mainfrom
73.Set-Matrix-Zeroes

Conversation

@tom4649

@tom4649 tom4649 commented Jul 20, 2026

Copy link
Copy Markdown
Owner

matrix[r][c] = 0

if first_row_has_zero:
matrix[0] = [0] * n

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

どこまでinplaceを期待するかですが、ここは厳密にはinplaceではないですね。(例えば、matrix[0]が他から参照されていなければ元のlistが別で残ってしまう)

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.

配列の参照自体も書き換えてしまいますね。matrix[0][:] = [0] * n に修正しました。

> A simple improvement uses O(m + n) space, but still not the best solution.
> Could you devise a constant space solution?

わからない

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私も10分考えてわからなかったので、このPRのコードを読みました。言われてみればそうだなとなりましたが、m+nのメモリ節約がどこまで必要かはなんとも言えないので、シビアでない用途であれば普通は別の変数で管理しますね。
とは言いつつ、一行目や一列目のステータスを退避しておいて、一行目や一列目をフラグやステータス管理として扱う、みたいな方法はたまにあるかもしれないです。今回は一行目や一列目のステータスを1変数にできるので、ステータス管理で余った空間を各行や各列のステータス管理に転用するというテクニックですね。

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.

自分は全く思いつきませんでした。「一行目や一列目をフラグやステータス管理」は頭に入れておこうと思います。

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.

2 participants