Neat and elegant solution using set in C++, Time Complexity O(n*m) and Space Complexity O(1)


#1

void Solution::setZeroes(vector<vector > &A) {
set col, row;
for(int i=0; i<A.size(); ++i) {
for(int j=0; j<A[0].size(); ++j){
if(A[i][j] == 0) {
col.insert(j);
row.insert(i);
}
}
}
for(auto c: col) {
for(int i=0; i<A.size(); ++i){
A[i][c] = 0;
}
}
for(auto r: row) {
for (int i=0; i< A[0].size(); ++i) {
A[r][i] = 0;
}
}
}


#2

how space is o(1) its max(m,n) because u make set variable.