Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 0073.Set-Matrix-Zeroes/memo.md
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?

わからない

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.

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


## 他の人のコード

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]をどちらかとみなせますね

読みにくくなると思うので二つフラグを持たせる方針でもよいのでは、と個人的には思う
17 changes: 17 additions & 0 deletions 0073.Set-Matrix-Zeroes/step1.py
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
17 changes: 17 additions & 0 deletions 0073.Set-Matrix-Zeroes/step1_revised.py
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
28 changes: 28 additions & 0 deletions 0073.Set-Matrix-Zeroes/step2.py
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

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 に修正しました。


if first_column_has_zero:
for r in range(m):
matrix[r][0] = 0
28 changes: 28 additions & 0 deletions 0073.Set-Matrix-Zeroes/step2_revised.py
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