Beginner Friendly Solution with Spiral Pattern Visualization 😊😊
Intuition\nThe code generates a spiral matrix by starting at a given point (rStart, cStart) and moving in a specific pattern: right, down, left, up. The pattern
# Intuition\nThe code generates a spiral matrix by starting at a given point (rStart, cStart) and moving in a specific pattern: right, down, left, up. The pattern expands outwards with each iteration, filling in the matrix.\n\n# Approach\n1. Initialization:\n- Create a result array res to store the coordinates of the spiral matrix.\n- Initialize the current row and column col with the starting position rStart and cStart.\n- Set initial values for rightDown and leftUp, which control the number of steps taken in each direction.\n2. Spiral Movement:\n- Iterate until the result array has size elements.\n- Iterate through the four directions (directions array):\n    - For right and down directions (i <= 1):\n        - Iterate rightDown times, moving in the current direction.\n        - Check if the new position is valid (within the matrix bounds). If so, add the coordinates to the result array.\n    - For left and up directions (i > 1):\n        - Iterate leftUp times, moving in the current direction.\n        - Check if the new position is valid (within the matrix bounds). If so, add the coordinates to the result array.\n3. Pattern Expansion:\n- After each iteration through the directions, increase rightDown and leftUp by 2 to expand the spiral pattern.\n4. Return Result:\n- Return the result array containing the coordinates of the spiral matrix.\n\n# Complexity\n- Time complexity: O(rows * cols) or O(size)\n\n- Space complexity: O(rows * cols) or O(size)\n\n# Visualization\n![Screenshot (309).png](https://assets.leetcode.com/users/images/c3c13829-3362-42c1-9b49-3b3c91cc6e93_1723096446.536668.png)\n- Pattern:\n    - right down left left up up\n    - right right right down down down left left left left up up up up\n    - ...\n\n\n# Code\n```\n/**\n * @param {number} rows\n * @param {number} cols\n * @param {number} rStart\n * @param {number} cStart\n * @return {number[][]}\n */\nconst directions = [\n  [0, 1], // right\n  [1, 0], // down\n  [0, -1], // left\n  [-1, 0], // up\n];\n\nvar spiralMatrixIII = function (rows, cols, rStart, cStart) {\n  const size = rows * cols;\n  const res = [[rStart, cStart]];\n\n  let row = rStart;\n  let col = cStart;\n  let rightDown = 1;\n  let leftUp = 2;\n  while (res.length < size) {\n    for (let i = 0; i < directions.length; i++) {\n      if (i <= 1) {\n        // right down\n        for (let j = 0; j < rightDown; j++) {\n          row += directions[i][0];\n          col += directions[i][1];\n\n          if (row >= 0 && row < rows && col >= 0 && col < cols) {\n            res.push([row, col]);\n          }\n        }\n      } else {\n        // left up\n        for (let j = 0; j < leftUp; j++) {\n          row += directions[i][0];\n          col += directions[i][1];\n\n          if (row >= 0 && row < rows && col >= 0 && col < cols) {\n            res.push([row, col]);\n          }\n        }\n      }\n    }\n\n    // update the pattern\n    rightDown += 2;\n    leftUp += 2;\n  }\n\n  return res;\n};\n\n```
['Array', 'Matrix', 'Simulation', 'JavaScript']