Skip to content

Commit 1903be8

Browse files
committed
✨ [54] Spiral Matrix
1 parent bd0be06 commit 1903be8

File tree

4 files changed

+212
-1
lines changed

4 files changed

+212
-1
lines changed

1187/my_solution.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @return {number}
55
*/
66
const makeArrayIncreasing = (arr1, arr2) => {
7+
// TODO: Incomplete ...
78
console.log(arr1)
89
console.log(arr2)
910

54/my_solution.js

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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)

54/solution.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number[]}
4+
*/
5+
const spiralOrder = (matrix) => {
6+
if (matrix.length === 1) return matrix[0];
7+
8+
let nextDirection = new Map(), formula = new Map();
9+
nextDirection.set("r", "b")
10+
nextDirection.set("b", "l")
11+
nextDirection.set("l", "t")
12+
nextDirection.set("t", "r")
13+
14+
formula.set("r", { r: 0, c: 1 })
15+
formula.set("b", { r: 1, c: 0 })
16+
formula.set("l", { r: 0, c: -1 })
17+
formula.set("t", { r: -1, c: 0 })
18+
19+
let spiralList = [];
20+
21+
const moveNext = (r, c, dir) => {
22+
let nextDir = nextDirection.get(dir);
23+
let { r: newR, c: newC } = formula.get(nextDir);
24+
25+
newR += r + (r === matrix.length ? -1 : 0) + (r < 0 ? 1 : 0)
26+
newC += c + (c === matrix[0].length ? -1 : 0) + (c < 0 ? 1 : 0)
27+
28+
return { r: newR, c: newC, dir: nextDir };
29+
}
30+
31+
const explore = (r, c, dir, tries) => {
32+
// edge
33+
if (
34+
r === matrix.length ||
35+
c === matrix[0].length ||
36+
r < 0 ||
37+
c < 0
38+
) {
39+
let next = moveNext(r, c, dir);
40+
explore(next.r, next.c, next.dir, tries);
41+
return;
42+
}
43+
44+
// visited - end
45+
if (matrix.length < tries) {
46+
return;
47+
}
48+
49+
if (Infinity === matrix[r][c]) {
50+
51+
let nextDir = nextDirection.get(dir);
52+
let { r: newR, c: newC } = formula.get(nextDir);
53+
54+
newR += r + (r === matrix.length ? -1 : 0) + (r < 0 ? 1 : 0) - ((formula.get(dir).r));
55+
newC += c + (c === matrix[0].length ? -1 : 0) + (c < 0 ? 1 : 0) - ((formula.get(dir).c));
56+
57+
explore(newR, newC, nextDir, tries + 1);
58+
return;
59+
}
60+
61+
spiralList.push(matrix[r][c]);
62+
matrix[r][c] = Infinity;
63+
64+
let dirFormula = formula.get(dir);
65+
explore(r + dirFormula.r, c + dirFormula.c, dir, tries)
66+
}
67+
68+
explore(0, 0, "r", 0);
69+
return spiralList;
70+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [33. Search in Rotated Sorted Array](./33/)
2929
- [49. Group Anagrams](./49/)
3030
- [53. Maximum Subarray](./53/)
31+
- [54. Spiral Matrix](./54/)
3132
- [56. Merge Intervals](./56/)
3233
- [57. Insert Interval](./57/)
3334
- [73. Set Matrix Zeroes](./73/)
@@ -117,7 +118,7 @@ Batch create:
117118
NOTE: JS IS HERE
118119
-->
119120
```ssh
120-
chapter=1187 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
121+
chapter=54 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
121122
```
122123
> then you can use `x` for quick debug.
123124

0 commit comments

Comments
 (0)