patternjavaMinor
If zero found in Matrix. make entire row & column zero
Viewed 0 times
entirecolumnmakefoundzeromatrixrow
Problem
Problem is - If in an MxN matrix, a zero is encountered, the entire row and entire column should be set to zero.
Example -
Should become:
I encountered this problem today and came up with a solution.
I'm hoping for some feedback regarding the time/space complexity and how efficient my solution is :
Code -
What I'm doing is:
-
Saving the coordinates of found zeroes in an ArrayList as a two
digit integer.
-
Taking out ones and tens places from the stored
list (which are the coordinates) and running a loop over the found
zeroes only.
Example -
8 4 2
2 3 7
1 0 1
9 8 4Should become:
8 0 2
2 0 7
0 0 0
9 0 4I encountered this problem today and came up with a solution.
I'm hoping for some feedback regarding the time/space complexity and how efficient my solution is :
Code -
public static void zeroMatrix(int[][] arr1)
{
ArrayList coord = new ArrayList<>();
int row = arr1.length;
int column = arr1[0].length;
for(int i=0; i < row; i++)
{
for(int j=0; j < column; j++)
{
if(arr1[i][j]==0)
{
coord.add((10*i) + j);
}
}
}
for(int n : coord)
{
int j=n%10;
int i=n/10; int k=0;
int l=0;
while(k<row)
{
arr1[k][j]=0;
k++;
}
while(l<column)
{
arr1[i][l]=0;
l++;
}
}
}What I'm doing is:
-
Saving the coordinates of found zeroes in an ArrayList as a two
digit integer.
-
Taking out ones and tens places from the stored
list (which are the coordinates) and running a loop over the found
zeroes only.
- Then making the corresponding rows and columns 0
Solution
Implementation
You did it right that you first find the zero entries and only after that set the rows/columns to zero. However, you can come up with more cohesive and flexible code:
Coding conventions
Braces
What comes to braces, the recommended way to write them is to put the opening brace (
you should write
Spaces
For the sake of readability, you should have a single space character after each
you should have
Program to interface
Not that it makes much difference in your method, but usually the rule goes that people should program to an interface, and not to an implementation. So instead of
you should write
Naming
Also
above I would have used the plural form:
Zero entry coordinates
As @200_success mentioned, packing the coordinates the way you do is not a good idea.
Hope that helps.
You did it right that you first find the zero entries and only after that set the rows/columns to zero. However, you can come up with more cohesive and flexible code:
public static void zeroMatrix(final int[][] matrix) {
findZeroEntries(matrix).forEach((p) -> {
setToZero(matrix, p.x, p.y);
});
}
private static List findZeroEntries(final int[][] matrix) {
final int height = matrix.length;
final int width = matrix[0].length;
final List zeroEntryList = new ArrayList<>();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (matrix[y][x] == 0) {
zeroEntryList.add(new Point(x, y));
}
}
}
return zeroEntryList;
}
private static void setToZero(final int[][] matrix,
final int x,
final int y) {
setRowToZero(matrix, y);
setColumnToZero(matrix, x);
}
private static void setRowToZero(final int[][] matrix, final int y) {
final int width = matrix[0].length;
for (int x = 0; x < width; ++x) {
matrix[y][x] = 0;
}
}
private static void setColumnToZero(final int[][] matrix, final int x) {
final int height = matrix.length;
for (int y = 0; y < height; ++y) {
matrix[y][x] = 0;
}
}Coding conventions
Braces
What comes to braces, the recommended way to write them is to put the opening brace (
{) on the same row with the statement that belongs to it. Instead offor (...)
{
...
}you should write
for (...) {
...
}Spaces
For the sake of readability, you should have a single space character after each
for and if, and a single space character before and after any binary operator. So instead offor(int i=0; ...) {
if(i==1) {
}
}you should have
for (int i = 0; ...) {
if (i == 1) {
...
}
}Program to interface
Not that it makes much difference in your method, but usually the rule goes that people should program to an interface, and not to an implementation. So instead of
ArrayList coord = new ArrayList<>();you should write
List coord = new ArrayList<>();Naming
arr1 is not the best possible name. Why not use, say, matrix?Also
int row = arr1.length;
int column = arr1[0].length;above I would have used the plural form:
int rows = arr1.length;
int columns = arr1[0].length;Zero entry coordinates
As @200_success mentioned, packing the coordinates the way you do is not a good idea.
Hope that helps.
Code Snippets
public static void zeroMatrix(final int[][] matrix) {
findZeroEntries(matrix).forEach((p) -> {
setToZero(matrix, p.x, p.y);
});
}
private static List<Point> findZeroEntries(final int[][] matrix) {
final int height = matrix.length;
final int width = matrix[0].length;
final List<Point> zeroEntryList = new ArrayList<>();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (matrix[y][x] == 0) {
zeroEntryList.add(new Point(x, y));
}
}
}
return zeroEntryList;
}
private static void setToZero(final int[][] matrix,
final int x,
final int y) {
setRowToZero(matrix, y);
setColumnToZero(matrix, x);
}
private static void setRowToZero(final int[][] matrix, final int y) {
final int width = matrix[0].length;
for (int x = 0; x < width; ++x) {
matrix[y][x] = 0;
}
}
private static void setColumnToZero(final int[][] matrix, final int x) {
final int height = matrix.length;
for (int y = 0; y < height; ++y) {
matrix[y][x] = 0;
}
}for (...)
{
...
}for (...) {
...
}for(int i=0; ...) {
if(i==1) {
}
}for (int i = 0; ...) {
if (i == 1) {
...
}
}Context
StackExchange Code Review Q#136226, answer score: 5
Revisions (0)
No revisions yet.