For a binary matrix of size n x m of 0's and 1's. eg
1 0 0 1
0 0 1 0
0 0 0 0
If a location has 1; make all the elements of that row and column = 1. eg
1 1 1 1
1 1 1 1
1 0 1 1
Solution should be with Time complexity = O(n*m) and space complexity = O(1)
Will post answer in comments. Try till then.
1 0 0 1
0 0 1 0
0 0 0 0
If a location has 1; make all the elements of that row and column = 1. eg
1 1 1 1
1 1 1 1
1 0 1 1
Solution should be with Time complexity = O(n*m) and space complexity = O(1)
Will post answer in comments. Try till then.
Take two variable
ReplyDeleterow0 = 0
col0 = 0
row0 += for (i=0 to n) arr[0][i]
col0 += for (i=0 to m) arr[i][0]
for ( i=0;i<n;i++)
for ( j=0;j<m;j++)
{
if ( arr[i][j])
{
arr[0][j] =1;
arr[i][0] =1;
break;
}
}
for ( i=1;i<n;i++)
{
if( arr[i][0])
for( j=1;j<m;j++)
arr[i][j] =1;
}
for ( j=1;j<m;j++)
{
if ( arr[0][j])
for( j=1;j<m;j++)
arr[i][j] =1;
}
if (row0) for(j=0;j<m;j++) arr[0][j] = 1;
if (col0) for(i=0;i<n;i++) arr[i][0] = 1;