Skip to content

[HoonDongKang] week 12 solutions #1596

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
Jun 21, 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
25 changes: 25 additions & 0 deletions non-overlapping-intervals/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* [Problem]: [435] Non-overlapping Intervals
* (https://leetcode.com/problems/non-overlapping-intervals/description/)
*/

//시간복잡도 O(n log n)
//공간복잡도 O(1)
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[1] - b[1]);

let count = 0;
let prev = intervals[0][1];

for (let i = 1; i < intervals.length; i++) {
let [start, end] = intervals[i];

if (prev > start) {
count++;
} else {
prev = end;
}
}

return count;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* [Problem]: [3651] Number of Connected Components in an Undirected Graph
* (https://www.lintcode.com/problem/3651/)
*/
export class Solution {
/**
* @param n: the number of vertices
* @param edges: the edges of undirected graph
* @return: the number of connected components
*/
countComponents(n: number, edges: number[][]): number {
//시간복잡도 O(n + e)
// 공간복잡도 O(n + e)
function dfsFunc(n: number, edges: number[][]): number {
const visited = new Array(n).fill(false);
const graph: number[][] = Array.from({ length: n }, () => []);

for (const [node, adjacent] of edges) {
graph[node].push(adjacent);
graph[adjacent].push(node);
}

function dfs(node: number) {
visited[node] = true;
for (const neighbor of graph[node]) {
if (!visited[neighbor]) {
dfs(neighbor);
}
}
}

let count = 0;

for (let i = 0; i < n; i++) {
if (!visited[i]) {
count++;
dfs(i);
}
}

return count;
}
// 시간복잡도 O(n + e)
// 공간복잡도 O(n + e)
function stackFunc(n: number, edges: number[][]): number {
Copy link
Contributor

Choose a reason for hiding this comment

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

DFS와 BFS 두 가지 풀이를 모두 공유해주셨네요! 🤩 두 가지 접근 모두 인접 리스트로 그래프를 구성함으로써 O(n+e)의 공간 복잡도를 가지는 것 같은데요,

union-find를 통해 edge들을 합쳐나간다면 그래프를 따로 구성하지 않고 O(n)의 공간 복잡도로 풀이할 수도 있다고 합니다! 유의미하게 복잡도를 최적화하는 것은 아니지만 다른 방법도 소개드리고자 공유드려요~!

const visited = new Array(n).fill(false);
const graph: number[][] = Array.from({ length: n }, () => []);
let count = 0;

for (const [node, adjacent] of edges) {
graph[node].push(adjacent);
graph[adjacent].push(node);
}

for (let i = 0; i < n; i++) {
if (visited[i]) continue;
count++;
const stack = [i];

while (stack.length) {
const node = stack.pop()!;
visited[node] = true;

for (let adj of graph[node]) {
if (!visited[adj]) stack.push(adj);
}
}
}

return count;
}
}
}
58 changes: 58 additions & 0 deletions remove-nth-node-from-end-of-list/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* [Problem]: [19] Remove Nth Node From End of List
* (https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)
*/

class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
// 시간복잡도 O(N)
// 공간복잡도 O(1)
function lengthFunc(head: ListNode | null, n: number): ListNode | null {
let length = 0;
let current = head;
while (current) {
length++;
current = current.next;
}

let dummy = new ListNode(0, head);
current = dummy;

for (let i = 0; i < length - n; i++) {
current = current?.next!;
}

current.next = current.next!.next || null;

return dummy.next;
}

// 시간복잡도 O(N)
// 공간복잡도 O(1)
function twoPointerFunc(head: ListNode | null, n: number): ListNode | null {
let first = head;

for (let i = 0; i < n; i++) {
first = first?.next!;
}

let dummy = new ListNode(0, head);
let second = dummy;

while (first) {
first = first.next;
second = second.next!;
}

second.next = second.next?.next || null;
return dummy.next;
}
}
43 changes: 43 additions & 0 deletions same-tree/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* [Problem]: [100] Same Tree
* (https://leetcode.com/problems/same-tree/description/)
*/

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;
}
}

function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
// 시간복잡도 O(N)
// 공간복잡도 O(N)
function recursiveFunc(p: TreeNode | null, q: TreeNode | null): boolean {
if (!p && !q) return true;
if (!p || !q) return false;
if (p.val !== q.val) return false;
return recursiveFunc(p.left, q.left) && recursiveFunc(p.right, q.right);
}

// 시간복잡도 O(N)
// 공간복잡도 O(N)
function stackFunc(p: TreeNode | null, q: TreeNode | null): boolean {
const stack = [[p, q]];
while (stack.length) {
const [p, q] = stack.pop()!;
if (!p && !q) continue;
if (!p || !q) return false;
if (p.val !== q.val) return false;

stack.push([p.left, q.left]);
stack.push([p.right, q.right]);
Copy link
Contributor

Choose a reason for hiding this comment

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

스택을 이용하여 반복문으로 풀 수도 있었군요! 덕분에 알고 갑니다 😄

}

return true;
}
}
54 changes: 54 additions & 0 deletions serialize-and-deserialize-binary-tree/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* [Problem]: [297] Serialize and Deserialize Binary Tree
* (\https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/)
*/

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;
}
}

/*
* Encodes a tree to a single string.
*/
//시간복잡도 O(N)
//공간복잡도 O(N)
function serialize(root: TreeNode | null): string {
if (!root) return "NULL";

const left = serialize(root.left);
const right = serialize(root.right);

return `${root.val},${left},${right}`;
}

/*
* Decodes your encoded data to tree.
*/
//시간복잡도 O(N)
//공간복잡도 O(N)
function deserialize(data: string): TreeNode | null {
const values = data.split(",");
let index = 0;

function dfs(): TreeNode | null {
if (values[index] === "NULL") {
index++;
return null;
}
const node = new TreeNode(+values[index++]);

node.left = dfs();
node.right = dfs();

return node;
}

return dfs();
}