Skip to content

Commit 42d3c39

Browse files
authored
Merge pull request #1040 from YeomChaeeun/main
[YeomChaeeun] Week 11
2 parents 28c7d56 + f3cf900 commit 42d3c39

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
/**
15+
* ํŠธ๋ฆฌ์˜ ๊นŠ์ด ๊ตฌํ•˜๊ธฐ
16+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
17+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
18+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
19+
* @param root
20+
*/
21+
function maxDepth(root: TreeNode | null): number {
22+
if(!root) return 0;
23+
let max = 0;
24+
let stack: [TreeNode | null, number][] = [[root, 1]];
25+
while(stack.length > 0) {
26+
const [node, depth] = stack.pop();
27+
max = Math.max(depth, max);
28+
if (node.left) stack.push([node.left, depth + 1]);
29+
if (node.right) stack.push([node.right, depth + 1]);
30+
}
31+
return max
32+
}

โ€Žmerge-intervals/YeomChaeeun.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„ ํ•ฉ์น˜๊ธฐ
3+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
4+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n log n) - sort() ์˜ํ–ฅ
5+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
6+
* @param intervals
7+
*/
8+
function merge(intervals: number[][]): number[][] {
9+
if(intervals.length === 1) return intervals
10+
11+
intervals.sort((a, b) => a[0] - b[0]);
12+
13+
let results: number[][] = []
14+
let [tempX, tempY] = intervals[0]
15+
16+
for(let i = 1; i < intervals.length; i++) {
17+
let [x, y] = intervals[i]
18+
if(x <= tempY) {
19+
tempY = Math.max(tempY, y)
20+
} else {
21+
results.push([tempX, tempY])
22+
tempX = x
23+
tempY = y
24+
}
25+
}
26+
results.push([tempX, tempY])
27+
28+
return results;
29+
}

โ€Žreorder-list/YeomChaeeun.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
/**
14+
Do not return anything, modify head in-place instead.
15+
*/
16+
17+
/**
18+
* ๋ฆฌ์ŠคํŠธ ์žฌ์ •๋ ฌ ํ•˜๊ธฐ (0 -> n -> 1 -> n-1 -> ...)
19+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
20+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
21+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
22+
* @param head
23+
*/
24+
function reorderList(head: ListNode | null): void {
25+
if (!head || !head.next) return;
26+
27+
const stack: ListNode[] = [];
28+
let node = head;
29+
while (node) {
30+
stack.push(node);
31+
node = node.next;
32+
}
33+
34+
let left = 0;
35+
let right = stack.length - 1;
36+
37+
while (left < right) {
38+
// ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋‹ค์Œ์— ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ ์—ฐ๊ฒฐ
39+
stack[left].next = stack[right];
40+
left++;
41+
42+
// ๋‚จ์€ ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ๋‹ค์Œ์— ๋‹ค์Œ ์™ผ์ชฝ ๋…ธ๋“œ ์—ฐ๊ฒฐ
43+
if (left < right) {
44+
stack[right].next = stack[left];
45+
right--;
46+
}
47+
}
48+
49+
// ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ next๋ฅผ null๋กœ ์„ค์ •
50+
stack[left].next = null;
51+
}

0 commit comments

Comments
ย (0)