-
-
Notifications
You must be signed in to change notification settings - Fork 194
[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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a6663c
add Same Tree solution
HoonDongKang a74c82a
add Remove Nth Node From End of List solution
HoonDongKang ab27f8d
add Number of Connected Components in an Undirected Graph solution
HoonDongKang 8a26cf0
add Non-overlapping Intervals solution
HoonDongKang d60724c
add Serialize and Deserialize Binary Tree solution
HoonDongKang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
73 changes: 73 additions & 0 deletions
73
number-of-connected-components-in-an-undirected-graph/HoonDongKang.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 스택을 이용하여 반복문으로 풀 수도 있었군요! 덕분에 알고 갑니다 😄 |
||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)의 공간 복잡도로 풀이할 수도 있다고 합니다! 유의미하게 복잡도를 최적화하는 것은 아니지만 다른 방법도 소개드리고자 공유드려요~!