Skip to content

Commit 1204a0a

Browse files
committed
week 06
1 parent 858b740 commit 1204a0a

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

longest-increasing-subsequence/Baekwangho.ts

Whitespace-only changes.

spiral-matrix/Baekwangho.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
(0,0) 에서 시작해서, 위 아래 끝값 및 왼쪽 오른쪽의 끝값을 갱신해가며
3+
현재 위치가 이보다 커질 경우 각각을 -1 하고 방향을 바꿔준다.
4+
*/
5+
function spiralOrder(matrix: number[][]): number[] {
6+
let hmin = 0,
7+
hmax = matrix[0].length - 1,
8+
vmin = 0,
9+
vmax = matrix.length - 1;
10+
let x = 0,
11+
y = 0;
12+
const result = [];
13+
let currentStep = [1, 0];
14+
15+
while (hmax >= hmin && vmax >= vmin) {
16+
result.push(matrix[y][x]);
17+
18+
const next = applyCurrentStep(currentStep, x, y, hmin, hmax, vmin, vmax);
19+
currentStep = next.nextStep;
20+
x = next.x;
21+
y = next.y;
22+
hmin = next.hmin;
23+
hmax = next.hmax;
24+
vmin = next.vmin;
25+
vmax = next.vmax;
26+
27+
// console.log(currentStep, x, y, hmin, hmax, vmin, vmax)
28+
}
29+
return result;
30+
}
31+
32+
function applyCurrentStep(
33+
currentStep: number[],
34+
x: number,
35+
y: number,
36+
hmin: number,
37+
hmax: number,
38+
vmin: number,
39+
vmax: number
40+
) {
41+
let nextStep = currentStep;
42+
43+
switch (true) {
44+
case currentStep[0] === 1 && currentStep[1] === 0: {
45+
if (x >= hmax) {
46+
nextStep = [0, 1];
47+
y += 1;
48+
vmin += 1;
49+
} else {
50+
x += 1;
51+
}
52+
break;
53+
}
54+
case currentStep[0] === 0 && currentStep[1] === 1: {
55+
if (y >= vmax) {
56+
nextStep = [-1, 0];
57+
x -= 1;
58+
hmax -= 1;
59+
} else {
60+
y += 1;
61+
}
62+
break;
63+
}
64+
case currentStep[0] === -1 && currentStep[1] === 0: {
65+
if (hmin >= x) {
66+
nextStep = [0, -1];
67+
y -= 1;
68+
vmax -= 1;
69+
} else {
70+
x -= 1;
71+
}
72+
break;
73+
}
74+
case currentStep[0] === 0 && currentStep[1] === -1: {
75+
if (vmin >= y) {
76+
nextStep = [1, 0];
77+
x += 1;
78+
hmin += 1;
79+
} else {
80+
y -= 1;
81+
}
82+
break;
83+
}
84+
}
85+
return { nextStep, x, y, hmin, hmax, vmin, vmax };
86+
}

0 commit comments

Comments
 (0)