O(mn)..but taking 90+ ms..


  • 0
    H
    class Solution {
    public:
        void setZeroes(vector<vector<int> > &matrix) {
            int m = matrix.size() , n = matrix[0].size();
            vector<int> row(m) , column(n);
            for(int i = 0 ; i < m ; i++){
                for(int j = 0; j < n ; j++){
                    if(matrix[i][j] == 0){
                        row[i] = 1;
                        column[j] = 1;
                    }
            }
        }
            for(int i = 0 ; i < m ; i++){
                if(row[i] == 1)
                    fillRow(i , matrix);
            }
            
            for(int j = 0 ; j < n ; j++){
                if(column[j] == 1)
                    fillColumn(j , matrix);
            }
            
        }
        
    private: 
    void fillRow(int row , vector<vector<int> > &matrix){
        int m = matrix[0].size();
        for(int j = 0 ; j < m ; j++){
            matrix[row][j] = 0;
        }
    }
    
    void fillColumn(int column , vector<vector<int> > &matrix){
        int n = matrix.size();
        
        for(int i = 0 ; i < n ; i++){
            matrix[i][column] = 0;
        }
    }
    
    };
    

    I am doing it using O(m+n) space and in O(mn) time but it is taking around 90ms .
    Can someone please suggest me where I can improve the complexity?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.