Problem solved python (O(n2))


#1

At first i was trying to solve using array to store the value of row and colum but while doing this i did mistake , and every time while appending colum or row value i checked if it is already in my list or not — And it leads to TLE!!!
Now I changed it to set() so …now it’s ok

IN THE BELOW CODE THE COMMENTED CODE IS MY 1ST TRY AND THE UPPER ONE IS CORRECT ONE --#########Happy Coding########–

class Solution:
    # @param A : list of list of integers
    # @return the same list modified
    def setZeroes(self, A):
        zrow = set()
        zcol =set()
        for i in range(len(A)):
            for j in range(len(A[0])):
                if A[i][j]==0:
                        zrow.add(i)
            
                        zcol.add(j)
                    
        for i in range(len(A)):
            for j in range(len(A[0])):
                if (i in zrow) or (j in zcol):
                    A[i][j]=0
        
        return A
            
            
# ANOTHER SOLUTION WHICH SHOWS TLE            
                
# class Solution:
#     # @param A : list of list of integers
#     # @return the same list modified
#     def setZeroes(self, A):
#         zrow = []
#         zcol = []
#         for i in range(len(A)):
#             for j in range(len(A[0])):
#                 if A[i][j]==0:
#                     if i not in zrow:
#                         zrow.append(i)
                        
#                     if j not in zcol:
#                         zcol.append(j)
                    
#         for i in range(len(A)):
#             for j in range(len(A[0])):
#                 if (i in zrow) or (j in zcol):
#                     A[i][j]=0
        
#         return A