Skip to content

Commit e623f67

Browse files
committed
feat: rotate-image solution
1 parent 1ac9481 commit e623f67

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

โ€Žrotate-image/YeomChaeeun.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
Do not return anything, modify matrix in-place instead.
3+
*/
4+
/**
5+
* 90๋„ ํšŒ์ „์‹œํ‚ค๊ธฐ
6+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
7+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n2)
8+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
9+
* @param matrix
10+
*/
11+
function rotate(matrix: number[][]): void {
12+
let n = matrix.length;
13+
14+
// 0,0 -> 2,0
15+
// 1,0 -> 2,1
16+
// 2,0 -> 2,2
17+
18+
// 0,1 -> 1,0
19+
// 1,1 -> 1,1
20+
// 2,1 -> 1,2
21+
22+
// 0,2 -> 0,0
23+
// 1,2 -> 0,1
24+
// 2,2 -> 0,2
25+
26+
// 1 2 3
27+
// 4 5 6
28+
// 7 8 9
29+
30+
// 1 4 7
31+
// 2 5 8
32+
// 3 6 9
33+
34+
// 7 4 1
35+
// 8 5 2
36+
// 9 6 3
37+
38+
// 1. ํ–‰๊ณผ ์—ด์„ ๋ฐ”๊ฟˆ(ํ–‰๊ณผ ์—ด์˜ ์ „์น˜)
39+
for (let i = 0; i < n; i++) {
40+
for (let j = i; j < n; j++) {
41+
// ๋Œ€๊ฐ์„ ์„ ๊ธฐ์ค€์œผ๋กœ ๋Œ€์นญ
42+
if (i !== j) {
43+
const temp = matrix[i][j];
44+
matrix[i][j] = matrix[j][i];
45+
matrix[j][i] = temp;
46+
}
47+
}
48+
}
49+
50+
// 2. ๊ฐ ํ–‰์„ ์ขŒ์šฐ๋กœ ๋’ค์ง‘๊ธฐ(= 90๋„ ํšŒ์ „)
51+
for (let i = 0; i < n; i++) {
52+
for (let j = 0; j < Math.floor(n / 2); j++) {
53+
const temp = matrix[i][j];
54+
matrix[i][j] = matrix[i][n - 1 - j];
55+
matrix[i][n - 1 - j] = temp;
56+
}
57+
}
58+
}

โ€Žvalidate-binary-search-tree/YeomChaeeun.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
1111
* }
1212
* }
1313
*/
14-
14+
/**
15+
* ์ด์ง„ํŠธ๋ฆฌ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌํ•˜๊ธฐ
16+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
17+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) - n: ๋…ธ๋“œ์˜ ์ด ๊ฐœ์ˆ˜
18+
* - ๊ณต๊ณค ๋ณต์žก๋„: O(h) - h: ํŠธ๋ฆฌ์˜ ๋†’์ด
19+
* @param root
20+
*/
1521
function isValidBST(root: TreeNode | null): boolean {
1622
if (!root) return true;
17-
23+
1824
function isValid(node: TreeNode | null, min: number, max: number): boolean {
1925
if (!node) return true;
2026
if (node.val <= min || node.val >= max) return false;
21-
22-
return isValid(node.left, min, node.val) &&
27+
28+
return isValid(node.left, min, node.val) &&
2329
isValid(node.right, node.val, max)
2430
}
2531

2632
// ์ดˆ๊ธฐ ํ˜ธ์ถœ (๋ฃจํŠธ ๋…ธ๋“œ์˜ ๋ฒ”์œ„๋Š” ๋ฌดํ•œ๋Œ€)
2733
return isValid(root, -Infinity, Infinity)
28-
}
34+
}

0 commit comments

Comments
ย (0)