I am currently trying to solve a problem where the input should be as following
int hours(int rows, int columns, List<List<Integer> > grid)
Where list grid is a Matrix of 0 and 1 ( 0 means not complete , 1 means complete ) as following:
0 1 1 0 1
0 1 0 1 0
0 0 0 0 1
0 1 0 0 0
Each value represent a machine in a network sending files to each other, So if the value is "1" then this node is able to send the files to ALL NEIGHBORS (Diagonal ones does not count only up/down/right/left).The issue is that once a 0 becomes a 1 ( affected by the neighbor cell ) it cannot send the file to any other neighbor before 1 HOUR.
The goal of the problem is to return how many hours shall it take before all the nodes receive the files? ( in other words all the Matrix becomes 1. Considering Run time complexity
For visual explanation: ( After the first hour this should be the state of the matrix which is considered as an iteration):
1 1 1 1 1
1 1 1 1 1
0 1 0 1 1
1 1 1 0 1
So, I followed an approach where I loop over the matrix looking in the provided grid and appending values in a temporary array ( as I don't know how to update or append the list values inside the main List). Then once the row index has reached the max i add 1 hour to an int variable and add values to the main grid.
I know the below code is not still working/complete and may have syntax mistakes but you get the idea and the approach.
My Question is, Is there any easier and efficient method than mine?? I also found a solution here But that only works with 2D arrays if my idea is worth it. However, wouldn't these 4 nested loop mess up the complexity of the code?
List<List<Integer>> grid2 = grid1;
boolean received= false;
int hours=0;
int rows_Temp = 0 ;
int columsTemp = 0 ;
int[][] grid2 = null ;
while(rows_Temp<rows&&!received)
{
if(rows_Temp==rows-1)
{
rows_Temp=0;
}
if(rows_Temp==0)
{
//create an array with the grid dimention
grid2= new int[rows][columns];
}
//manage top left corner
if(rows_Temp==0 && columsTemp == 0 )
{
//find right & down
int center= grid.get(rows_Temp).get(columsTemp);
int right = grid.get(rows_Temp).get(columsTemp+1);
int down = grid.get(rows_Temp+1).get(columsTemp);
if(center==1)
{
if(right==0)
{
grid2[rows_Temp][columsTemp+1] = 1;
}
if(down==0)
{
grid2[rows_Temp+1][columsTemp]=1;
}
}
}
//manage top right corner
else if(rows_Temp==0 && columsTemp == columns-1)
{
//find left and down
int center= grid.get(rows_Temp).get(columsTemp);
int left = grid.get(rows_Temp).get(columsTemp-1);
int down = grid.get(rows_Temp+1).get(columsTemp);
if(center==1)
{
if(left==0)
{
grid2[rows_Temp][columsTemp-1] = 1;
}
if(down==0)
{
grid2[rows_Temp+1][columsTemp]=1;
}
}
}
//mange down left corner of the array
else if(rows_Temp==rows-1 && columsTemp == 0)
{
//find up and right
int center= grid.get(rows_Temp).get(columsTemp);
int right = grid.get(rows_Temp).get(columsTemp+1);
int up = grid.get(rows_Temp-1).get(columsTemp);
if(center==1)
{
if(right==0)
{
grid2[rows_Temp][columsTemp+1] = 1;
}
if(up==0)
{
grid2[rows_Temp-1][columsTemp]=1;
}
}
}
//manage down right corner
else if(rows_Temp==rows-1 && columsTemp == columns-1)
{
//find left and up
int center= grid.get(rows_Temp).get(columsTemp);
int left = grid.get(rows_Temp).get(columsTemp-1);
int up = grid.get(rows_Temp-1).get(columsTemp);
if(center==1)
{
if(left==0)
{
grid2[rows_Temp][columsTemp-1] = 1;
}
if(up==0)
{
grid2[rows_Temp-1][columsTemp]=1;
}
}
}
//manage left sides but not corners
else if(rows_Temp!=0&& rows_Temp!=rows-1&& columsTemp==0)
{
int center= grid.get(rows_Temp).get(columsTemp);
int right = grid.get(rows_Temp).get(columsTemp+1);
int up = grid.get(rows_Temp-1).get(columsTemp);
int down = grid.get(rows_Temp+1).get(columsTemp);
if(center==1)
{
if(right==0)
{
grid2[rows_Temp][columsTemp+1] = 1;
}
if(up==0)
{
grid2[rows_Temp-1][columsTemp]=1;
}
if(down==0)
{
grid2[rows_Temp+1][columsTemp]=1;
}
}
}
if(columsTemp==columns-1)
{
columsTemp=0;
rows_Temp++;
System.out.println();
}
else
{
columsTemp++;
}
}
System.out.println("------------");
return 0 ;
}