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
In #2624 Snail Traversal, the goal is to convert a 1D array into a 2D matrix with rowsCount rows and colsCount columns using a vertical zigzag (snail-like) pattern. Before constructing the matrix, verify that rowsCount * colsCount == arr.length. If this condition fails, it is impossible to build the matrix.
The key idea is to fill the matrix column by column. For even-indexed columns, insert elements from top to bottom. For odd-indexed columns, insert elements from bottom to top. This alternating direction creates the characteristic "snail" traversal pattern. Maintain a pointer in the input array and update it as elements are placed into the matrix.
This approach processes each element exactly once, making it efficient. The algorithm runs in O(n) time where n is the length of the array, and uses O(rowsCount × colsCount) space for the resulting matrix.
This approach involves directly filling a 2D array using the input array nums by iterating over the columns in tandem with rows, alternating between top-down and bottom-up directions for each new column.
The key idea is to iterate over the given nums array, and fill the current column based on whether its index is even or odd. For even indices we fill from the top row to the bottom row; for odd indices, from the bottom row to the top row.
The algorithm involves:
The C solution first checks if product of rows and columns equals the size of the nums array. It initializes a dynamic 2D array to store the resulting matrix. Using a loop, it fills each column in either top-to-bottom or bottom-to-top order depending on the current column index, incrementing through the nums array sequentially.
Java
Python
JavaScript
C#
Time Complexity: O(ROWS * COLS) as each element must be traversed once.
Space Complexity: O(ROWS * COLS) for storing the result matrix.
This second approach draws on precomputing indices for traversing the nums array according to snail traversal rules using a two-pointer technique. Here, two pointers always mark top and bottom traversals of a given column.
In this strategy:
The C++ solution uses vectors to dynamically allocate and manage the rows of matrices. With two pointers, iterating up or down depending on even or odd column indices results in efficient walking through each column without extra list operations.
C#
Time Complexity: O(ROWS * COLS)
Space Complexity: O(ROWS * COLS) due to resulting vector matrix.
| Approach | Complexity |
|---|---|
| Matrix Construction Using Snail Order with Iterative Filling | Time Complexity: O(ROWS * COLS) as each element must be traversed once. |
| Two-Pointer Technique with Precomputed Indices | Time Complexity: O(ROWS * COLS) |
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Column-wise Zigzag Matrix Construction | O(n) | O(rowsCount × colsCount) |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,262 views views
Watch 9 more video solutions →Practice Snail Traversal with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor