Solving the spiral matrix algorithm in JavaScript

·

3 min read

Solving the spiral matrix algorithm in JavaScript

What is a spiral matrix?

The Spiral Matrix problem takes a 2-Dimensional array of N-rows and M-columns as an input, and prints the elements of this matrix in spiral order. The spiral begins at the top left corner of the input matrix, and prints the elements it encounters, while looping towards the centre of this matrix, in a clockwise manner.

This was one of the hardest algorithms so far, it took some time for it to click to how I should approach solving this algorithm. Once I figured out I needed to go from one corner point to the next without revisiting the previous node, I started to figure a way to go from top left corner → top right corner → bottom right corner → bottom left corner. From here I just needed to repeat the process

matrix.gif

Instructions

Write a function that accepts an integer N and returns NxN spiral matrix.

Note: You are able to assign values to indices that have not yet been assigned in an array.

const arr = []
arr[3] = 4 // [null,null,null, 4]

Create empty array of arrays called 'results'

const results = []
for (let i = 0; i < n; i++) {
    results.push([])
}

Create a counter variable, starting at 1

let counter = 1

Create variables to track, start row, end row, start column and end column

  let startColumn = 0
  let endColumn = n - 1
  let startRow = 0
  let endRow = n - 1

As long as (start column ≤ end column) AND (start row ≤ end row

while (startColumn <= endColumn && startRow <= endRow) {
    //Code here
}

Top Row

Loop from start column to end column

At results[startRow][i] assign counter variable

Increment counter

Increment start now

while (startColumn <= endColumn && startRow <= endRow) {
    //top row
    for (let i = startColumn; i <= endColumn; i++) {
      results[startRow][i] = counter
      counter++
    }
    startRow++
}

Right column

Loop from start row to end row

At results[i][endColumn] assign counter variable

Increment Counter

Decrement end row

while (startColumn <= endColumn && startRow <= endRow) {
    //top row
    for (let i = startColumn; i <= endColumn; i++) {
      results[startRow][i] = counter
      counter++
    }
    startRow++
        //Right column
    for (let i = startRow; i <= endRow; i++) {
      results[i][endColumn] = counter
      counter++
    }
    endColumn--
}

...repeat for other two sides

while (startColumn <= endColumn && startRow <= endRow) {
    //top row
    for (let i = startColumn; i <= endColumn; i++) {
      results[startRow][i] = counter
      counter++
    }
    startRow++

    //Right column
    for (let i = startRow; i <= endRow; i++) {
      results[i][endColumn] = counter
      counter++
    }
    endColumn--

    // Bottom row
    for (let i = endColumn; i >= startColumn; i--) {
      results[endRow][i] = counter
      counter++
    }
    endRow--

    // start column
    for (let i = endRow; i >= startRow; i--) {
      results[i][startColumn] = counter
      counter++
    }
    startColumn++
  }

Finally return the results

Full Code

const matrix = (n) => {
  const results = []
  for (let i = 0; i < n; i++) {
    results.push([])
  }
  let counter = 1
  let startColumn = 0
  let endColumn = n - 1
  let startRow = 0
  let endRow = n - 1
  while (startColumn <= endColumn && startRow <= endRow) {
    //top row
    for (let i = startColumn; i <= endColumn; i++) {
      results[startRow][i] = counter
      counter++
    }
    startRow++

    //Right column
    for (let i = startRow; i <= endRow; i++) {
      results[i][endColumn] = counter
      counter++
    }
    endColumn--

    // Bottom row
    for (let i = endColumn; i >= startColumn; i--) {
      results[endRow][i] = counter
      counter++
    }
    endRow--

    // start column
    for (let i = endRow; i >= startRow; i--) {
      results[i][startColumn] = counter
      counter++
    }
    startColumn++
  }
  console.log(results)
  return results
}