73. Set Matrix Zeroes - python3

shsh·2021년 1월 8일
0

leetcode

목록 보기
71/161

73. Set Matrix Zeroes

Given an m x n matrix. If an element is 0, set its entire row and column to 0. Do it in-place.

Follow up:

  • A straight forward solution using O(mn) space is probably a bad idea.
  • A simple improvement uses O(m + n) space, but still not the best solution.
    Could you devise a constant space solution?

My Answer 1: Accepted (Runtime: 116 ms - 99.18% / Memory Usage: 14.9 MB - 97.07%)

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        indexi = set()
        indexj = set()
        
        for i in range(0, len(matrix)):
            for j in range(0, len(matrix[i])):
                if matrix[i][j] == 0:
                    indexi.add(i)
                    indexj.add(j)
                    
        for i in range(0, len(matrix)):
            for j in range(0, len(matrix[i])):
                if i in indexi or j in indexj:
                    matrix[i][j] = 0

오직 내 머리로 풀어서 99%가 나온 게 얼마만인건지...

0인 값들의 index i, j 를 저장할 set 를 각각 만들어서 set 안에 인덱스가 있으면 0으로 바꿔주었다
(set 는 중복 제거)

심지어 솔루션 Approach 1: Additional Memory Approach 과 똑같다!!!! 야호

근데..

Time Complexity: O(M×N) where M and N are the number of rows and columns respectively.

Space Complexity: O(M+N).

라네요..^^

O(1) Space, Efficient Solution

Solution 1: Runtime: 128 ms - 70.67% / Memory Usage: 15 MB - 64.05%

class Solution(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        is_col = False
        R = len(matrix)
        C = len(matrix[0])
        for i in range(R):
            # Since first cell for both first row and first column is the same i.e. matrix[0][0]
            # We can use an additional variable for either the first row/column.
            # For this solution we are using an additional variable for the first column
            # and using matrix[0][0] for the first row.
            if matrix[i][0] == 0:
                is_col = True
            for j in range(1, C):
                # If an element is zero, we set the first element of the corresponding row and column to 0
                if matrix[i][j]  == 0:
                    matrix[0][j] = 0
                    matrix[i][0] = 0

        # Iterate over the array once again and using the first row and first column, update the elements.
        for i in range(1, R):
            for j in range(1, C):
                if not matrix[i][0] or not matrix[0][j]:
                    matrix[i][j] = 0

        # See if the first row needs to be set to zero as well
        if matrix[0][0] == 0:
            for j in range(C):
                matrix[0][j] = 0

        # See if the first column needs to be set to zero as well        
        if is_col:
            for i in range(R):
                matrix[i][0] = 0

예의상 O(1) 솔루션을 가져왔읍니다.

profile
Hello, World!

0개의 댓글

관련 채용 정보