ajayg415 / Javascript-Algorithms

1 stars 0 forks source link

Problem: Spiral Traverse #24

Open ajayg415 opened 3 years ago

ajayg415 commented 3 years ago

Write a function that takes in an n x m two-dimensional array (that can be square-shaped when n == m) and returns a one-dimensional array of all the array's elements in spiral order.

Spiral order starts at the top left corner of the two-dimensional array, goes to the right, and proceeds in a spiral pattern all the way until every element has been visited.

Sample Input
array = [
  [1,   2,  3, 4],
  [12, 13, 14, 5],
  [11, 16, 15, 6],
  [10,  9,  8, 7],
]

Sample Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

O(n) time | O(n) space - where n is the total number of elements in the array

ajayg415 commented 3 years ago
function spiralTraverse(array) {
  let res = [];
  let startRow = 0;
  let endRow = array.length - 1;
  let startCol = 0;
  let endCol = array[0].length - 1;
  while(startRow <= endRow && startCol <=  endCol){
    for(let col = startCol; col <= endCol; col++){
      res.push(array[startRow][col])    
    }
    for(let row = startRow+1; row <= endRow; row++){
      res.push(array[row][endCol])
    }
    for(let col = endCol-1;col>=startCol;col--){
      if(startRow === endRow) break;
      res.push(array[endRow][col])
    }
    for(let row = endRow-1; row>startRow;row--){
      if(startCol === endCol) break;
      res.push(array[row][startCol])
    }
    startRow++;
    endRow--;
    startCol++;
    endCol--;
  }
  return res;
}

@msreddy09 , @krishna63 check the solution

ajayg415 commented 3 years ago
Test Case 1
{
  "array": [
    [1, 2, 3, 4],
    [12, 13, 14, 5],
    [11, 16, 15, 6],
    [10, 9, 8, 7]
  ]
}
Test Case 2
{
  "array": [
    [1]
  ]
}
Test Case 3
{
  "array": [
    [1, 2],
    [4, 3]
  ]
}
Test Case 4
{
  "array": [
    [1, 2, 3],
    [8, 9, 4],
    [7, 6, 5]
  ]
}
Test Case 5
{
  "array": [
    [19, 32, 33, 34, 25, 8],
    [16, 15, 14, 13, 12, 11],
    [18, 31, 36, 35, 26, 9],
    [1, 2, 3, 4, 5, 6],
    [20, 21, 22, 23, 24, 7],
    [17, 30, 29, 28, 27, 10]
  ]
}
Test Case 6
{
  "array": [
    [4, 2, 3, 6, 7, 8, 1, 9, 5, 10],
    [12, 19, 15, 16, 20, 18, 13, 17, 11, 14]
  ]
}
Test Case 7
{
  "array": [
    [27, 12, 35, 26],
    [25, 21, 94, 11],
    [19, 96, 43, 56],
    [55, 36, 10, 18],
    [96, 83, 31, 94],
    [93, 11, 90, 16]
  ]
}
Test Case 8
{
  "array": [
    [1, 2, 3, 4],
    [10, 11, 12, 5],
    [9, 8, 7, 6]
  ]
}
Test Case 9
{
  "array": [
    [1, 2, 3],
    [12, 13, 4],
    [11, 14, 5],
    [10, 15, 6],
    [9, 8, 7]
  ]
}
Test Case 10
{
  "array": [
    [1, 11],
    [2, 12],
    [3, 13],
    [4, 14],
    [5, 15],
    [6, 16],
    [7, 17],
    [8, 18],
    [9, 19],
    [10, 20]
  ]
}
Test Case 11
{
  "array": [
    [1, 3, 2, 5, 4, 7, 6]
  ]
}
Test Case 12
{
  "array": [
    [1],
    [3],
    [2],
    [5],
    [4],
    [7],
    [6]
  ]
}