Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 73 · Matrix

Set Matrix Zeroes

LeetCode 73 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Use row 0 / column 0 as markers, plus a col0 flag.

ಮಧ್ಯಮArrayHash TableMatrix
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Set Matrix Zeroes (LeetCode 73) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Matrix ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

First record which rows and columns contain a zero, then clear them. Separate marker arrays are simplest; the optimal version stores the markers in the first row and column of the matrix itself, with one extra flag for column 0.

Approach

  1. 1

    Version 1: boolean arrays for rows and columns, then zero any marked cell.

  2. 2

    Version 2: mark matrix[i][0] and matrix[0][j]; column 0 gets its own flag.

  3. 3

    Zero the inner cells using the markers.

  4. 4

    Finally zero row 0 and column 0 if their markers say so.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ

1
Marker arraysO(m·n) time · O(m+n) space

Separate row and column flags.

2
In-place markersAcceptedO(m·n) time · O(1) space

First row / column as flags.

Marker arraysPythonO(m·n) time · O(m+n) space
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        m = len(matrix)
        n = len(matrix[0])
        row_marker = [False]*m
        col_marker = [False]*n
        for i in range(m):
            for j in range(n):
                if matrix[i][j]==0:
                    row_marker[i]=True
                    col_marker[j]=True
        for i in range(m):
            for j in range(n):
                if row_marker[i]==True or col_marker[j]==True:
                    matrix[i][j]=0
In-place markersPythonO(m·n) time · O(1) space
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        m = len(matrix)
        n = len(matrix[0])
        col0 = 1
        for i in range(m):
            for j in range(n):
                if matrix[i][j]==0:
                    matrix[i][0]=0
                    if j!=0:
                        matrix[0][j]=0
                    else:
                        col0=0
        for i in range(1,m):
            for j in range(1,n):
                if matrix[i][0]==0 or matrix[0][j]==0:
                    matrix[i][j]=0
        #first row
        for j in range(n):
            if matrix[0][0]==0:
                matrix[0][j]=0
        
        #first col
        for i in range(m):
            if col0==0:
                matrix[i][0]=0
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Set Matrix Zeroes — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Set Matrix Zeroes ಹಿಂದಿನ intuition ಏನು?

First record which rows and columns contain a zero, then clear them. Separate marker arrays are simplest; the optimal version stores the markers in the first row and column of the matrix itself, with one extra flag for column 0.

Set Matrix Zeroes (LeetCode 73) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Version 1: boolean arrays for rows and columns, then zero any marked cell. 2. Version 2: mark matrix[i][0] and matrix[0][j]; column 0 gets its own flag. 3. Zero the inner cells using the markers. 4. Finally zero row 0 and column 0 if their markers say so.

Set Matrix Zeroes ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(m·n). Space: O(1) in the final version. (Accepted approach: In-place markers.)

Set Matrix Zeroes ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Process row 0 and column 0 last, or the markers get destroyed.

Set Matrix Zeroes ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?

Marker arrays (O(m·n) time · O(m+n) space): Separate row and column flags. In-place markers (O(m·n) time · O(1) space): First row / column as flags.