LeetCode 2020 Dec 7 Generate Spiral Matrix
Problem Statement : We are given with number of rows and numbers of columns. We have to generate the matrix in the spiral way. Here we will be given with N with this we have to generate N * N matrix.
The question is Leetcode.
Input The N will be given. we have to check N >=1
Pseudo Code
- We need to have a variable (insertedNUmbers) which we will be using for inserting the number at any cell of the matrix.
- For this example we can take number of rows and columns also to be filled be 4.
- We need two other variables which will contain current iterating row and column. Let we call them as currentRowIndex for Row and currentCOlIndex for Column.
- We have to iterate till the current Row Index (currentRowIndex) is less than number of rows (noOfRows) and current column index is less the number of columns(noOfColumns).
- For the first time we will be filling the very first row. While filling the row, the row index which has to be filled will be constant, and the column index alone will change. We will be filling this row with currentRowIndex, and the index of column will start with currentCOlIndex, it will be iterated till number of colums.
- So we will fill the first row with the values 1,2,3,4 from the left to right fashion.
- Now we will be incrementing the currentRowIndex by one, so next time when we come we wll be filling the next row.
- Then the next for loop will be to fill from top to bottom. Here the column index will be static and the row will be changing. We will be starting from the current Row index, because already the value 4 will be filled in matrix at the cell[0][3], so we have to fill [1][3]. Now we will be iterating till number of colums (in our case its 4).
- So we will have the values 5,6,7 filled at the last column. Now we will be decreasing the number of columns to be generated by 1.
- The next step is to generate the last row (in first iteration) from right to left. Already the the last cell of the matrix i.e., [3][3] is filled with 7. So we have to start from one cell previous to it.
- Here the static value will be the row which we are iterating, in this case it will be N-1; i.e., row[3] according to computer terminology( because for us it starts from 0).
- We will be using the Number of Column value and will be reducing till the currentColIndex value. so the values 8,9,10 will be generated.
- Then we will decrement number of rows.
- Next we have to fill the the first colum (for first iteration) from bottom to top. Now the column will be static and we will be starting with noOfColums-1, we will be iteating till the current Index of column. so the value 11, 12 will be generated.
In the First while loop the matrix will be like below
1 2 3 4 12 - - 5 11 - - 6 10 9 8 7
Now we will again start the same while loop and generate the values 13,14, 15, 16.
The output matrix with N=4 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
Java Code
public int[][] generateMatrix(int n) {
return generateMatrix(n,n);
}
private int[][] generateMatrix(int noOfRows, int noOfColumns){
if(noOfRows == 0 || noOfColumns ==0){
return new int[0][0];
}
int currentRowIndex = 0;
int currentCOlIndex = 0;
int insertedNUmbers =1;
int[][] matrixToReturn = new int[noOfRows][noOfColumns];
while(currentRowIndex <noOfRows && currentCOlIndex<noOfColumns){
// FIlls row from left to right
for(int colToFill=currentCOlIndex; colToFill<noOfColumns;colToFill++){
matrixToReturn[currentRowIndex][colToFill] = insertedNUmbers++;
}
currentRowIndex = currentRowIndex+1;
// Fills column from top to bottom
for(int rowValToFill = currentRowIndex; rowValToFill <noOfRows; rowValToFill++){
matrixToReturn[rowValToFill][noOfColumns-1] = insertedNUmbers++;
}
noOfColumns = noOfColumns -1;
// Fills row from right to left
if(currentRowIndex<noOfRows){
for(int colToFill = noOfColumns-1; colToFill>=currentCOlIndex;--colToFill){
matrixToReturn[noOfRows-1][colToFill] = insertedNUmbers++;
}
noOfRows = noOfRows -1;
}
// FIlls column from bottom to top
if(currentCOlIndex<noOfColumns){
for(int rowValToFill = noOfRows-1; rowValToFill>=currentRowIndex;--rowValToFill){
matrixToReturn[rowValToFill][currentCOlIndex] = insertedNUmbers++;
}
currentCOlIndex = currentCOlIndex+1;
}
}
return matrixToReturn;
}