-
Notifications
You must be signed in to change notification settings - Fork 0
Set Matrix Zeroes #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
73.Set-Matrix-Zeroes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Set Matrix Zeroes #154
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # 73. Set Matrix Zeroes | ||
|
|
||
| ## step1 | ||
| 空間計算量O(n + m), 時間計算量O(mn)の解法。 | ||
|
|
||
| > A simple improvement uses O(m + n) space, but still not the best solution. | ||
| > Could you devise a constant space solution? | ||
|
|
||
| わからない | ||
|
|
||
| ## 他の人のコード | ||
|
|
||
| https://leetcode.com/problems/set-matrix-zeroes/solutions/6121398/video-o1-space-use-the-first-row-and-col-1kr7/ | ||
|
|
||
| 空間O(1)解法 | ||
|
|
||
| https://github.com/thonda28/leetcode/pull/9 | ||
|
|
||
| > 意味のある変数名にしたいです。num_rowsとかでしょうか? | ||
|
|
||
| 問題文にあわせて m, nにしたがこちらの方がわかりやすいかもしれない | ||
|
|
||
| > 0行目と0列目のフラグをそれぞれ用意しなくても、matrix[0][0]をどちらかとみなせますね | ||
|
|
||
| 読みにくくなると思うので二つフラグを持たせる方針でもよいのでは、と個人的には思う | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution: | ||
| def setZeroes(self, matrix: list[list[int]]) -> None: | ||
| row_with_zero = set() | ||
| column_with_zero = set() | ||
|
|
||
| for index_row, row in enumerate(matrix): | ||
| for index_column, value in enumerate(row): | ||
| if value == 0: | ||
| row_with_zero.add(index_row) | ||
| column_with_zero.add(index_column) | ||
|
|
||
| for index_row, row in enumerate(matrix): | ||
| if index_row in row_with_zero: | ||
| row[:] = [0] * len(row) | ||
| continue | ||
| for index_column in column_with_zero: | ||
| row[index_column] = 0 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution: | ||
| def setZeroes(self, matrix: list[list[int]]) -> None: | ||
| row_with_zero = set() | ||
| column_with_zero = set() | ||
|
|
||
| for r, row in enumerate(matrix): | ||
| for c, value in enumerate(row): | ||
| if value == 0: | ||
| row_with_zero.add(r) | ||
| column_with_zero.add(c) | ||
|
|
||
| for r, row in enumerate(matrix): | ||
| if r in row_with_zero: | ||
| row[:] = [0] * len(row) | ||
| continue | ||
| for c in column_with_zero: | ||
| row[c] = 0 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| class Solution: | ||
| def setZeroes(self, matrix: list[list[int]]) -> None: | ||
| if not matrix: | ||
| return | ||
|
|
||
| m = len(matrix) | ||
| n = len(matrix[0]) | ||
|
|
||
| first_row_has_zero = 0 in matrix[0] | ||
| first_column_has_zero = any(matrix[r][0] == 0 for r in range(m)) | ||
|
|
||
| for r in range(1, m): | ||
| for c in range(1, n): | ||
| if matrix[r][c] == 0: | ||
| matrix[r][0] = 0 | ||
| matrix[0][c] = 0 | ||
|
|
||
| for r in range(1, m): | ||
| for c in range(1, n): | ||
| if matrix[r][0] == 0 or matrix[0][c] == 0: | ||
| matrix[r][c] = 0 | ||
|
|
||
| if first_row_has_zero: | ||
| matrix[0] = [0] * n | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. どこまでinplaceを期待するかですが、ここは厳密にはinplaceではないですね。(例えば、matrix[0]が他から参照されていなければ元のlistが別で残ってしまう)
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 配列の参照自体も書き換えてしまいますね。 |
||
|
|
||
| if first_column_has_zero: | ||
| for r in range(m): | ||
| matrix[r][0] = 0 | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| class Solution: | ||
| def setZeroes(self, matrix: list[list[int]]) -> None: | ||
| if not matrix: | ||
| return | ||
|
|
||
| m = len(matrix) | ||
| n = len(matrix[0]) | ||
|
|
||
| first_row_has_zero = 0 in matrix[0] | ||
| first_column_has_zero = any(matrix[r][0] == 0 for r in range(m)) | ||
|
|
||
| for r in range(1, m): | ||
| for c in range(1, n): | ||
| if matrix[r][c] == 0: | ||
| matrix[r][0] = 0 | ||
| matrix[0][c] = 0 | ||
|
|
||
| for r in range(1, m): | ||
| for c in range(1, n): | ||
| if matrix[r][0] == 0 or matrix[0][c] == 0: | ||
| matrix[r][c] = 0 | ||
|
|
||
| if first_row_has_zero: | ||
| matrix[0][:] = [0] * n | ||
|
|
||
| if first_column_has_zero: | ||
| for r in range(m): | ||
| matrix[r][0] = 0 |
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.
There was a problem hiding this comment.
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変数にできるので、ステータス管理で余った空間を各行や各列のステータス管理に転用するというテクニックですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分は全く思いつきませんでした。「一行目や一列目をフラグやステータス管理」は頭に入れておこうと思います。