Skip to content

Commit bdeab31

Browse files
authored
Merge pull request #1075 from mike2ox/main
[moonhyeok] Week 13
2 parents 40f6718 + 7e7086f commit bdeab31

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

โ€Žinsert-interval/mike2ox.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Source: https://leetcode.com/problems/insert-interval/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: ๋ฏธ๋ฆฌ ์ •๋ ฌ ํ›„ ๋ ๋ถ€๋ถ„๋งŒ ๋น„๊ตํ•˜๋ฉด์„œ ๊ฐฑ์‹ ์‹œํ‚ค๊ธฐ
4+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(nlogn) - ์ •๋ ฌ๋•Œ๋ฌธ์—
5+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
*
7+
* ํ†ต๊ณผ์‹œ๊ฐ„
8+
* - ์ตœ์ดˆ: 40๋ถ„
9+
*/
10+
function insert(intervals: number[][], newInterval: number[]): number[][] {
11+
const mergedIntervals = [...intervals, newInterval];
12+
const result: number[][] = [];
13+
mergedIntervals.sort((a, b) => a[0] - b[0]);
14+
15+
for (const interval of mergedIntervals) {
16+
// ๊ฒฐ๊ณผ ๋ฐฐ์—ด์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ํ˜„์žฌ ๊ตฌ๊ฐ„์ด ๋งˆ์ง€๋ง‰ ๊ตฌ๊ฐ„๊ณผ ๊ฒน์น˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
17+
if (result.length === 0 || interval[0] > result[result.length - 1][1]) {
18+
result.push(interval);
19+
} else {
20+
// ๊ฒฐ๊ณผ๋ฌผ์˜ ๋งˆ์ง€๋ง‰ ๊ตฌ๊ฐ„์˜ ํฐ๊ฐ’ ๋ฒ”์œ„์™€ ํ˜„์žฌ ๊ตฌ๊ฐ„์˜ ํฐ๊ฐ’ ๋ฒ”์œ„๋ฅผ ๋น„๊ตํ›„ ํฐ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•˜๊ธฐ
21+
result[result.length - 1][1] = Math.max(
22+
result[result.length - 1][1],
23+
interval[1]
24+
);
25+
}
26+
}
27+
28+
return result;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Source: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree
3+
* Solution: DFS๋ฅผ ์ด์šฉํ•ด์„œ ๋‘ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•ด ์ˆœ์ฐจ์ ์œผ๋กœ ๋น„๊ตํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋™์ผ ๋…ธ๋“œ๋ฅผ ์ถ”์ถœ
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(N) - ์ตœ์•…์ธ ๊ฒฝ์šฐ, ์ „์ฒด ๋…ธ๋“œ์ˆ˜ ํƒ์ƒ‰
6+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(N) - ์ตœ์•…์ธ ๊ฒฝ์šฐ, ์ „์ฒด ๋…ธ๋“œ์ˆ˜ ๋ณด๊ด€
7+
*
8+
* ๋‹ค๋ฅธ ํ’€์ด
9+
* - ์žฌ๊ท€๋กœ๋„ ํ•ด๊ฒฐํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ด์ง€๋งŒ ๋ฐ”๋กœ ๊ตฌํ˜„์ฒด๊ฐ€ ๋– ์˜ค๋ฅด์ง€ ์•Š์Œ
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* class TreeNode {
15+
* val: number
16+
* left: TreeNode | null
17+
* right: TreeNode | null
18+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
19+
* this.val = (val===undefined ? 0 : val)
20+
* this.left = (left===undefined ? null : left)
21+
* this.right = (right===undefined ? null : right)
22+
* }
23+
* }
24+
*/
25+
26+
function lowestCommonAncestor(
27+
root: TreeNode | null,
28+
p: TreeNode | null,
29+
q: TreeNode | null
30+
): TreeNode | null {
31+
if (!root || !p || !q) return null;
32+
let stack = new Array(root);
33+
let pRoute: Array<TreeNode> | null;
34+
let qRoute: Array<TreeNode> | null;
35+
let answer: TreeNode | null;
36+
const visited = new Set();
37+
38+
while (stack.length) {
39+
const left = stack.at(-1).left;
40+
if (left && !visited.has(left.val)) {
41+
stack.push(left);
42+
continue;
43+
}
44+
const right = stack.at(-1).right;
45+
if (right && !visited.has(right.val)) {
46+
stack.push(right);
47+
continue;
48+
}
49+
const now = stack.pop();
50+
visited.add(now.val);
51+
if (now.val === q.val) {
52+
qRoute = [...stack, now];
53+
continue;
54+
}
55+
if (now.val === p.val) {
56+
pRoute = [...stack, now];
57+
continue;
58+
}
59+
}
60+
const shortLength =
61+
pRoute.length > qRoute.length ? qRoute.length : pRoute.length;
62+
for (let i = 0; i < shortLength; i++) {
63+
if (pRoute.at(i) !== qRoute.at(i)) {
64+
answer = pRoute.at(i - 1);
65+
break;
66+
}
67+
}
68+
return answer ? answer : pRoute.at(shortLength - 1);
69+
}

โ€Žtwo-sum/mike2ox.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* Source: https://leetcode.com/problems/two-sum/
2+
* Source: https://leetcode.com/problems/insert-interval/
3+
34
* ํ’€์ด๋ฐฉ๋ฒ•: Map์„ ์ด์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๋‚˜๋จธ์ง€ ์ˆซ์ž๋ฅผ ์ €์žฅํ•˜๋ฉด์„œ ํ™•์ธ
45
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
56
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

0 commit comments

Comments
ย (0)