-
-
Save astambi/ca990efbb6b00c410ff300dd8538ab39 to your computer and use it in GitHub Desktop.
Spiral Matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function spiralMatrix([arr]) { | |
printMatrix(getMatrix(arr)); | |
function getMatrix(arr) { | |
let [rows, cols] = arr.split(' ') | |
.filter(x => x != '') | |
.map(Number); | |
let [count, maxCount, minRow, minCol, maxRow, maxCol] = [0, rows * cols, 0, 0, rows-1, cols-1]; | |
let matrix = []; | |
for (let r = 0; r < rows; r++) matrix[r] = []; | |
while (count < maxCount) { | |
for (let c = minCol; c <= maxCol && count < maxCount; c++) matrix[minRow][c] = ++count; | |
minRow++; | |
for (let r = minRow; r <= maxRow && count < maxCount; r++) matrix[r][maxCol] = ++count; | |
maxCol--; | |
for (let c = maxCol; c >= minCol && count < maxCount; c--) matrix[maxRow][c] = ++count; | |
maxRow--; | |
for (let r = maxRow; r >= minRow && count < maxCount; r--) matrix[r][minCol] = ++count; | |
minCol++; | |
} | |
return matrix; | |
} | |
function printMatrix(matrix) { | |
matrix.forEach(row => console.log(row.join(' '))); | |
// console.log(matrix.map(row => row.join(' ')).join('\n')); | |
} | |
} | |
spiralMatrix(['5 5']); | |
spiralMatrix(['3 3']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment