Snail Traversal Problem
Description
LeetCode Problem 2624.
Write code that enhances all arrays such that you can call the snail(rowsCount, colsCount) method that transforms the 1D array into a 2D array organised in the pattern known as snail traversal order. Invalid input values should output an empty array. If rowsCount * colsCount !== nums.length, the input is considered invalid.
Snail traversal order starts at the top left cell with the first value of the current array. It then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column, until the entire current array is covered. For example, when given the input array [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15] with rowsCount = 5 and colsCount = 4, the desired output matrix is shown below. Note that iterating the matrix following the arrows corresponds to the order of numbers in the original array.

Example 1:
Input:
nums = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]
rowsCount = 5
colsCount = 4
Output:
[
[19,17,16,15],
[10,1,14,4],
[3,2,12,20],
[7,5,18,11],
[9,8,6,13]
]
Example 2:
Input:
nums = [1,2,3,4]
rowsCount = 1
colsCount = 4
Output: [[1, 2, 3, 4]]
Example 3:
Input:
nums = [1,3]
rowsCount = 2
colsCount = 2
Output: []
Explanation: 2 multiplied by 2 is 4, and the original array [1,3] has a length of 2; therefore, the input is invalid.
Constraints:
0 <= nums.length <= 2501 <= nums[i] <= 10001 <= rowsCount <= 2501 <= colsCount <= 250
Solution
Approach: Walk the columns, flipping direction
First reject bad input: if rowsCount * colsCount !== nums.length, return []. Otherwise allocate the matrix and place the values one by one in the order given, moving along a column.
Keep the current cell (row, col) and a vertical step dir that is +1 (down) or -1 (up). After writing a value, move row by dir. If that step leaves the grid (row -1 or rowsCount), undo it, flip the direction and move one column to the right instead. That is exactly the "down the first column, up the next, down the next..." pattern. A single-row grid works too, because every step immediately bumps into a boundary and shifts one column.
Complexity: O(n) time and O(n) space for the output matrix, where n is the array length.
/**
* @param {number} rowsCount
* @param {number} colsCount
* @return {Array<Array<number>>}
*/
Array.prototype.snail = function (rowsCount, colsCount) {
if (rowsCount * colsCount !== this.length) {
return [];
}
const grid = Array.from({ length: rowsCount }, () => new Array(colsCount));
let row = 0;
let col = 0;
let dir = 1;
for (const value of this) {
grid[row][col] = value;
row += dir;
if (row === rowsCount || row === -1) {
row -= dir; // step back inside the grid
dir = -dir; // reverse direction
col++; // and move to the next column
}
}
return grid;
};
/**
* const arr = [1,2,3,4];
* arr.snail(1,4); // [[1,2,3,4]]
*/