{ "data": { "question": { "questionId": "2760", "questionFrontendId": "2624", "boundTopicId": null, "title": "Snail Traversal", "titleSlug": "snail-traversal", "content": "

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.

\n\n

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.

\n\n

 

\n\n

\"Traversal

\n\n

 

\n

Example 1:

\n\n
\nInput: \nnums = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]\nrowsCount = 5\ncolsCount = 4\nOutput: \n[\n [19,17,16,15],\n [10,1,14,4],\n [3,2,12,20],\n [7,5,18,11],\n [9,8,6,13]\n]\n
\n\n

Example 2:

\n\n
\nInput: \nnums = [1,2,3,4]\nrowsCount = 1\ncolsCount = 4\nOutput: [[1, 2, 3, 4]]\n
\n\n

Example 3:

\n\n
\nInput: \nnums = [1,3]\nrowsCount = 2\ncolsCount = 2\nOutput: []\nExplanation: 2 multiplied by 2 is 4, and the original array [1,3] has a length of 2; therefore, the input is invalid.\n
\n\n

 

\n

Constraints:

\n\n\n\n

 

\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 87, "dislikes": 40, "isLiked": null, "similarQuestions": "[{\"title\": \"Array Prototype Last\", \"titleSlug\": \"array-prototype-last\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Array Upper Bound\", \"titleSlug\": \"array-upper-bound\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "exampleTestcases": "[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]\n5\n4\n[1,2,3,4]\n1\n4\n[1,3]\n2\n2", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} rowsCount\n * @param {number} colsCount\n * @return {Array>}\n */\nArray.prototype.snail = function(rowsCount, colsCount) {\n \n}\n\n/**\n * const arr = [1,2,3,4];\n * arr.snail(1,4); // [[1,2,3,4]]\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "declare global {\n interface Array {\n snail(rowsCount: number, colsCount: number): number[][];\n }\n}\n\nArray.prototype.snail = function(rowsCount: number, colsCount: number): number[][] {\n \n}\n\n/**\n * const arr = [1,2,3,4];\n * arr.snail(1,4); // [[1,2,3,4]]\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"7.3K\", \"totalSubmission\": \"11.3K\", \"totalAcceptedRaw\": 7345, \"totalSubmissionRaw\": 11285, \"acRate\": \"65.1%\"}", "hints": [ "Different ways to approach this problem. Perhaps store a boolean if you are moving up or down and a current column. Reverse the direction and increment the column every time you hits a wall.", "Is there a way way to do this without storing state - by just using math?" ], "solution": { "id": "1956", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]\n5\n4", "metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"rowsCount\",\n \"type\": \"integer\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"colsCount\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"nums\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 5.1.6, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }