Skip to content

Commit e581128

Browse files
committed
Sync LeetCode submission Runtime - 6 ms (27.26%), Memory - 18.5 MB (53.65%)
1 parent 67cb405 commit e581128

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

0073-set-matrix-zeroes/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p>
2+
3+
<p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg" style="width: 450px; height: 169px;" />
8+
<pre>
9+
<strong>Input:</strong> matrix = [[1,1,1],[1,0,1],[1,1,1]]
10+
<strong>Output:</strong> [[1,0,1],[0,0,0],[1,0,1]]
11+
</pre>
12+
13+
<p><strong class="example">Example 2:</strong></p>
14+
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg" style="width: 450px; height: 137px;" />
15+
<pre>
16+
<strong>Input:</strong> matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
17+
<strong>Output:</strong> [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>m == matrix.length</code></li>
25+
<li><code>n == matrix[0].length</code></li>
26+
<li><code>1 &lt;= m, n &lt;= 200</code></li>
27+
<li><code>-2<sup>31</sup> &lt;= matrix[i][j] &lt;= 2<sup>31</sup> - 1</code></li>
28+
</ul>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Follow up:</strong></p>
32+
33+
<ul>
34+
<li>A straightforward solution using <code>O(mn)</code> space is probably a bad idea.</li>
35+
<li>A simple improvement uses <code>O(m + n)</code> space, but still not the best solution.</li>
36+
<li>Could you devise a constant space solution?</li>
37+
</ul>

0073-set-matrix-zeroes/solution.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Approach 2: Use first row and column
2+
3+
class Solution(object):
4+
def setZeroes(self, matrix: List[List[int]]) -> None:
5+
is_col = False
6+
R = len(matrix)
7+
C = len(matrix[0])
8+
for i in range(R):
9+
# Since first cell for both first row and first column is the same i.e. matrix[0][0]
10+
# We can use an additional variable for either the first row/column.
11+
# For this solution we are using an additional variable for the first column
12+
# and using matrix[0][0] for the first row.
13+
if matrix[i][0] == 0:
14+
is_col = True
15+
for j in range(1, C):
16+
# If an element is zero, we set the first element of the corresponding row and column to 0
17+
if matrix[i][j] == 0:
18+
matrix[0][j] = 0
19+
matrix[i][0] = 0
20+
21+
# Iterate over the array once again and using the first row and first column, update the elements.
22+
for i in range(1, R):
23+
for j in range(1, C):
24+
if not matrix[i][0] or not matrix[0][j]:
25+
matrix[i][j] = 0
26+
27+
# See if the first row needs to be set to zero as well
28+
if matrix[0][0] == 0:
29+
for j in range(C):
30+
matrix[0][j] = 0
31+
32+
# See if the first column needs to be set to zero as well
33+
if is_col:
34+
for i in range(R):
35+
matrix[i][0] = 0
36+

0 commit comments

Comments
 (0)