Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Dynamic-Programming/spiralMatrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @description Return spiral matrix.
* @param {Number} total number of row and column.
* @returns {Array [Number][]}
*/
function spiralMatrix(N) {
const result = []

for (let i = 0; i < N; i++) {
result.push(new Array(N).fill(null))
}

let top = 0,
level = 0
let count = 1

while (true) {
result[level][top] = count

const upLevel = result[level - 1],
bottomLevel = result[level + 1],
leftLevel = result[level],
rightLevel = result[level]

const up = upLevel ? upLevel[top] : undefined
const bottom = bottomLevel ? bottomLevel[top] : undefined
const left = leftLevel ? leftLevel[top - 1] : undefined
const right = rightLevel ? rightLevel[top + 1] : undefined

if (right === null && up !== null) {
top++
} else if (right === null && up === null) {
level--
} else if (right !== null && bottom === null) {
level++
} else if (right !== null && bottom !== null && left === null) {
top--
} else if (right !== null && bottom !== null && up === null) {
level--
} else {
break
}

count++
}

return result
}

module.exports = spiralMatrix;
21 changes: 21 additions & 0 deletions Dynamic-Programming/tests/spiralMatrix.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'vitest'
import spiralMatrix from '../spiralMatrix'

describe('spiralMatrix', () => {
test('should return spiral matrix', () => {
const result = spiralMatrix(3)
const expected = [
[1, 2, 3],
[8, 9, 4],
[7, 6, 5]
]

expect(result.length).toBe(expected.length)

for (let i = 0; i < expected.length; i++) {
for (let j = 0; j < expected[i].length; j++) {
expect(result[i][j]).toBe(expected[i][j])
}
}
})
});
Loading