MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / spiralOrder

Function spiralOrder

javascript/0054-spiral-matrix.js:9–64  ·  view source on GitHub ↗
(matrix, order = [])

Source from the content-addressed store, hash-verified

7 * @return {number[]}
8 */
9var spiralOrder = function (matrix, order = []) {
10 const [rows, cols] = [matrix.length - 1, matrix[0].length - 1];
11 let [top, bot, left, right] = [0, rows, 0, cols];
12
13 const isInBounds = () => left <= right && top <= bot;
14 while (isInBounds()) {
15 /* Time O(ROWS * COLS) */
16 addTop(
17 matrix,
18 top,
19 bot,
20 left,
21 right,
22 order,
23 ); /* Time O(COLS) | Ignore Auxilary Spsace O(ROWS * COLS) */
24 top++;
25
26 addRight(
27 matrix,
28 top,
29 bot,
30 left,
31 right,
32 order,
33 ); /* Time O(ROWS) | Ignore Auxilary Spsace O(ROWS * COLS) */
34 right--;
35
36 const hasRow = top <= bot;
37 if (hasRow) {
38 addBot(
39 matrix,
40 top,
41 bot,
42 left,
43 right,
44 order,
45 ); /* Time O(COLS) | Ignore Auxilary Spsace O(ROWS * COLS) */
46 bot--;
47 }
48
49 const hasCol = left <= right;
50 if (hasCol) {
51 addLeft(
52 matrix,
53 top,
54 bot,
55 left,
56 right,
57 order,
58 ); /* Time O(ROWS) | Ignore Auxilary Spsace O(ROWS * COLS) */
59 left++;
60 }
61 }
62
63 return order;
64};
65
66var addTop = (matrix, top, bot, left, right, order) => {

Callers

nothing calls this directly

Calls 8

addTopFunction · 0.85
addRightFunction · 0.85
addBotFunction · 0.85
addLeftFunction · 0.85
initOrderFunction · 0.85
spiralFunction · 0.85
isInBoundsFunction · 0.70
traverseFunction · 0.70

Tested by

no test coverage detected