|
| 1 | +/** |
| 2 | + * @param {number[][]} matrix |
| 3 | + * @return {number[]} |
| 4 | + */ |
| 5 | +const spiralOrder = (matrix) => { |
| 6 | + if (matrix.length === 1) return matrix[0]; |
| 7 | + let nextDirection = new Map(), formula = new Map(); |
| 8 | + nextDirection.set("r", "b") |
| 9 | + nextDirection.set("b", "l") |
| 10 | + nextDirection.set("l", "t") |
| 11 | + nextDirection.set("t", "r") |
| 12 | + |
| 13 | + formula.set("r", { r: 0, c: 1 }) |
| 14 | + formula.set("b", { r: 1, c: 0 }) |
| 15 | + formula.set("l", { r: 0, c: -1 }) |
| 16 | + formula.set("t", { r: -1, c: 0 }) |
| 17 | + |
| 18 | + console.log(nextDirection) |
| 19 | + console.log(formula) |
| 20 | + |
| 21 | + let spiralList = []; |
| 22 | + |
| 23 | + const moveNext = (r, c, dir) => { |
| 24 | + let nextDir = nextDirection.get(dir); |
| 25 | + let { r: newR, c: newC } = formula.get(nextDir); |
| 26 | + console.log(`>> Going newR:${r + newR + (r === matrix.length ? -1 : 0)}, newC:${c + newC + (c === matrix[0].length ? -1 : 0)}`) |
| 27 | + // let isREdge = r > matrix.length, isCEdge = c > matrix[0].length; |
| 28 | + console.log("matrix[0].length ") |
| 29 | + console.log(matrix[0].length) |
| 30 | + |
| 31 | + newR += r + (r === matrix.length ? -1 : 0) + (r < 0 ? 1 : 0) |
| 32 | + newC += c + (c === matrix[0].length ? -1 : 0) + (c < 0 ? 1 : 0) |
| 33 | + |
| 34 | + return { r: newR, c: newC, dir: nextDir }; |
| 35 | + } |
| 36 | + |
| 37 | + const explore = (r, c, dir, tries) => { |
| 38 | + console.log(`> r:${r}, c:${c}, dir:${dir}, tries:${tries}`) |
| 39 | + // edge |
| 40 | + if ( |
| 41 | + r === matrix.length || |
| 42 | + c === matrix[0].length || |
| 43 | + r < 0 || |
| 44 | + c < 0 |
| 45 | + ) { |
| 46 | + let next = moveNext(r, c, dir); |
| 47 | + explore(next.r, next.c, next.dir, tries); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // visited - end |
| 52 | + if (matrix.length < tries) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (Infinity === matrix[r][c]) { |
| 57 | + // let next = moveNext(r, c, dir); |
| 58 | + // explore(next.r, next.c, next.dir, tries + 1); |
| 59 | + |
| 60 | + let nextDir = nextDirection.get(dir); |
| 61 | + let { r: newR, c: newC } = formula.get(nextDir); |
| 62 | + console.log(`>> Going newR:${r + newR + (r === matrix.length ? -1 : 0)}, newC:${c + newC + (c === matrix[0].length ? -1 : 0)}`) |
| 63 | + // let isREdge = r > matrix.length, isCEdge = c > matrix[0].length; |
| 64 | + console.log("matrix[0].length ") |
| 65 | + console.log(matrix[0].length) |
| 66 | + |
| 67 | + newR += r + (r === matrix.length ? -1 : 0) + (r < 0 ? 1 : 0) - ((formula.get(dir).r)); |
| 68 | + newC += c + (c === matrix[0].length ? -1 : 0) + (c < 0 ? 1 : 0) - ((formula.get(dir).c)); |
| 69 | + |
| 70 | + // return { r: newR, c: newC, dir: nextDir }; |
| 71 | + explore(newR, newC, nextDir, tries + 1); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + spiralList.push(matrix[r][c]); |
| 76 | + matrix[r][c] = Infinity; |
| 77 | + |
| 78 | + let dirFormula = formula.get(dir); |
| 79 | + // console.log(`> r:${r}, c:${c}, addR:${dirFormula.r}, addC:${dirFormula.c}`) |
| 80 | + // if next we turn |
| 81 | + // matrix[r + dirFormula.r][c + dirFormula.c]; |
| 82 | + explore(r + dirFormula.r, c + dirFormula.c, dir, tries) |
| 83 | + } |
| 84 | + |
| 85 | + explore(0, 0, "r", 0); |
| 86 | + console.log(matrix) |
| 87 | + return spiralList; |
| 88 | +}; |
| 89 | + |
| 90 | +let x = null; |
| 91 | + |
| 92 | +// [1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,99,98,97,96,95,94,93,92,91,81,71,61,51,41,31,21,11,12,13,14,15,16,17,18,19,29,39,49,59,69,79,89,88,87,86,85,84,83,82,72,62,52,42,32,22,23,24,25,26,27,28,38,48,58,68,78,77,76,75,74,73,63,53,43,33,34,35,36,37,47,57,67,66,65,64,54,44,45,46,56,55] |
| 93 | +x = spiralOrder([ |
| 94 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 95 | + [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], |
| 96 | + [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], |
| 97 | + [31, 32, 33, 34, 35, 36, 37, 38, 39, 40], |
| 98 | + [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], |
| 99 | + [51, 52, 53, 54, 55, 56, 57, 58, 59, 60], |
| 100 | + [61, 62, 63, 64, 65, 66, 67, 68, 69, 70], |
| 101 | + [71, 72, 73, 74, 75, 76, 77, 78, 79, 80], |
| 102 | + [81, 82, 83, 84, 85, 86, 87, 88, 89, 90], |
| 103 | + [91, 92, 93, 94, 95, 96, 97, 98, 99, 100] |
| 104 | +]); |
| 105 | + |
| 106 | +// [1,2,3,4,5,10,15,20,25,24,23,22,21,16,11,6,7,8,9,14,19,18,17,12,13] |
| 107 | +// x = spiralOrder([ |
| 108 | +// [1, 2, 3, 4, 5], |
| 109 | +// [6, 7, 8, 9, 10], |
| 110 | +// [11, 12, 13, 14, 15], |
| 111 | +// [16, 17, 18, 19, 20], |
| 112 | +// [21, 22, 23, 24, 25] |
| 113 | +// ]); |
| 114 | + |
| 115 | +// x = spiralOrder([[1]]); |
| 116 | + |
| 117 | +// [1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10] |
| 118 | +// x = spiralOrder([ |
| 119 | +// [1, 2, 3, 4], |
| 120 | +// [5, 6, 7, 8], |
| 121 | +// [9, 10, 11, 12], |
| 122 | +// [13, 14, 15, 16] |
| 123 | +// ]); |
| 124 | + |
| 125 | +// [1,2,3,4,8,12,11,10,9,5,6,7] |
| 126 | +// x = spiralOrder([ |
| 127 | +// [1, 2, 3, 4], |
| 128 | +// [5, 6, 7, 8], |
| 129 | +// [9, 10, 11, 12] |
| 130 | +// ]); |
| 131 | + |
| 132 | +// [1,2,3,6,9,8,7,4,5] |
| 133 | +// x = spiralOrder([ |
| 134 | +// [1, 2, 3], |
| 135 | +// [4, 5, 6], |
| 136 | +// [7, 8, 9] |
| 137 | +// ]); |
| 138 | +console.log("> R") |
| 139 | +console.log(x) |
0 commit comments