I solved this problem in about 11 minutes (with all 50+ test cases passed).
I think most of you already know me by now, and I truly appreciate your advice, but please be brutal and judge me as if I was being interviewed by you.
Question:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Code:
public void setZeroes(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
boolean[][] flagArr = new boolean[rows][cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(matrix[i][j]==0){
flagArr[i][j]=true;
}
}
}
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(flagArr[i][j]==true){
/*for rows*/
for(int k=0; k<rows; k++){
matrix[k][j]=0;
}
/*for cols*/
for(int z=0; z<cols; z++){
matrix[i][z]=0;
}
}
}
}
}