public void rewrite(int[][] matrix) {
    validateInput(matrix);

    Set<Integer> rowsToSkip = new HashSet<Integer>();
    Set<Integer> columnsToSkip = new HashSet<Integer>();
    for (int i = 0; i < matrix.length; i++) {
      for (int j = 0; j < matrix[i].length; j++) {
        if (shouldPaintRowAndColumn(matrix, i, j, rowsToSkip, columnsToSkip)) {
          rewriteRow(matrix, i, rowsToSkip);
          rewriteColumn(matrix, j, columnsToSkip);
        }
      }
    }
  }
Iterative algorithm. The complexity order of this solution in time terms is equals to O(N*M)
where N and M are the height and width of the matrix. In space terms, the complexity order of
this algorithm has a linear relation to the number of zeros in the matrix and the size of
this, at the end, equals to O(N*M) because we are using two auxiliary data structures to keep
temporal data which is going to grow following a linear relation to the matrix size.
src/main/java/com/github/pedrovgs/problem55/RewriteRowAndColumnsWithZeros.java