Skip to content

[moonhyeok] Week 13 #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions insert-interval/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Source: https://leetcode.com/problems/insert-interval/
* 풀이방법: 미리 정렬 후 끝 부분만 비교하면서 갱신시키기
* 시간복잡도: O(nlogn) - 정렬때문에
* 공간복잡도: O(n)
*
* 통과시간
* - 최초: 40분
*/
function insert(intervals: number[][], newInterval: number[]): number[][] {
const mergedIntervals = [...intervals, newInterval];
const result: number[][] = [];
mergedIntervals.sort((a, b) => a[0] - b[0]);

for (const interval of mergedIntervals) {
// 결과 배열이 비어있거나 현재 구간이 마지막 구간과 겹치지 않는 경우
if (result.length === 0 || interval[0] > result[result.length - 1][1]) {
result.push(interval);
} else {
// 결과물의 마지막 구간의 큰값 범위와 현재 구간의 큰값 범위를 비교후 큰 값으로 대체하기
result[result.length - 1][1] = Math.max(
result[result.length - 1][1],
interval[1]
);
}
}

return result;
}
69 changes: 69 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Source: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree
* Solution: DFS를 이용해서 두 노드까지의 경로를 구해 순차적으로 비교하면서 가장 마지막 동일 노드를 추출
*
* 시간복잡도: O(N) - 최악인 경우, 전체 노드수 탐색
* 공간복잡도: O(N) - 최악인 경우, 전체 노드수 보관
*
* 다른 풀이
* - 재귀로도 해결할 것으로 보이지만 바로 구현체가 떠오르지 않음
*/

/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최초 통과 시간: 42분

function lowestCommonAncestor(
root: TreeNode | null,
p: TreeNode | null,
q: TreeNode | null
): TreeNode | null {
if (!root || !p || !q) return null;
let stack = new Array(root);
let pRoute: Array<TreeNode> | null;
let qRoute: Array<TreeNode> | null;
let answer: TreeNode | null;
const visited = new Set();

while (stack.length) {
const left = stack.at(-1).left;
if (left && !visited.has(left.val)) {
stack.push(left);
continue;
}
const right = stack.at(-1).right;
if (right && !visited.has(right.val)) {
stack.push(right);
continue;
}
const now = stack.pop();
visited.add(now.val);
if (now.val === q.val) {
qRoute = [...stack, now];
continue;
}
if (now.val === p.val) {
pRoute = [...stack, now];
continue;
}
}
const shortLength =
pRoute.length > qRoute.length ? qRoute.length : pRoute.length;
for (let i = 0; i < shortLength; i++) {
if (pRoute.at(i) !== qRoute.at(i)) {
answer = pRoute.at(i - 1);
break;
}
}
return answer ? answer : pRoute.at(shortLength - 1);
}
3 changes: 2 additions & 1 deletion two-sum/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Source: https://leetcode.com/problems/two-sum/
* Source: https://leetcode.com/problems/insert-interval/

* 풀이방법: Map을 이용하여 필요한 나머지 숫자를 저장하면서 확인
* 시간복잡도: O(n)
* 공간복잡도: O(n)
Expand Down