Set Matrix Zeroes#154
Open
tom4649 wants to merge 2 commits into
Open
Conversation
| matrix[r][c] = 0 | ||
|
|
||
| if first_row_has_zero: | ||
| matrix[0] = [0] * n |
There was a problem hiding this comment.
どこまでinplaceを期待するかですが、ここは厳密にはinplaceではないですね。(例えば、matrix[0]が他から参照されていなければ元のlistが別で残ってしまう)
Owner
Author
There was a problem hiding this comment.
配列の参照自体も書き換えてしまいますね。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? | ||
|
|
||
| わからない |
There was a problem hiding this comment.
私も10分考えてわからなかったので、このPRのコードを読みました。言われてみればそうだなとなりましたが、m+nのメモリ節約がどこまで必要かはなんとも言えないので、シビアでない用途であれば普通は別の変数で管理しますね。
とは言いつつ、一行目や一列目のステータスを退避しておいて、一行目や一列目をフラグやステータス管理として扱う、みたいな方法はたまにあるかもしれないです。今回は一行目や一列目のステータスを1変数にできるので、ステータス管理で余った空間を各行や各列のステータス管理に転用するというテクニックですね。
Owner
Author
There was a problem hiding this comment.
自分は全く思いつきませんでした。「一行目や一列目をフラグやステータス管理」は頭に入れておこうと思います。
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.
https://leetcode.com/problems/set-matrix-zeroes/