-
-
Notifications
You must be signed in to change notification settings - Fork 244
[hyer0705] Week 12 solution #1937
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
+163
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
07f122a
same tree solution
hyer0705 df9f1ed
remove nth node from end of list solution
hyer0705 dfff6ef
number of connected components in an undirected graph solution
hyer0705 7e329b3
non-overlapping intervals solution
hyer0705 bc998d2
serialize and deserialize binary tree solution
hyer0705 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,18 @@ | ||
function eraseOverlapIntervals(intervals: number[][]): number { | ||
intervals.sort((a, b) => a[1] - b[1]); | ||
|
||
let removedCount = 0; | ||
let prevEnd = intervals[0][1]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
const [start, end] = intervals[i]; | ||
|
||
if (prevEnd > start) { | ||
removedCount++; | ||
} else { | ||
prevEnd = end; | ||
} | ||
} | ||
|
||
return removedCount; | ||
} |
36 changes: 36 additions & 0 deletions
36
number-of-connected-components-in-an-undirected-graph/hyer0705.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,36 @@ | ||
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 { | ||
const parent = Array.from({ length: n }, (_, i) => i); | ||
let components = n; | ||
|
||
const find = (i: number): number => { | ||
if (parent[i] === i) { | ||
return i; | ||
} | ||
|
||
parent[i] = find(parent[i]); | ||
return parent[i]; | ||
}; | ||
|
||
const union = (a: number, b: number): void => { | ||
const rootA = find(a); | ||
const rootB = find(b); | ||
|
||
if (rootA !== rootB) { | ||
parent[rootA] = rootB; | ||
components--; | ||
} | ||
}; | ||
|
||
for (const [a, b] of edges) { | ||
union(a, b); | ||
} | ||
|
||
return components; | ||
} | ||
} |
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,34 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* 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 { | ||
if (!head) return null; | ||
|
||
let current = head; | ||
let nodeLen = 0; | ||
while (current.next) { | ||
current = current.next; | ||
nodeLen++; | ||
} | ||
|
||
if (nodeLen - n < 0) return head.next; | ||
|
||
current = head; | ||
let count = 0; | ||
while (count < nodeLen - n) { | ||
current = current.next; | ||
count++; | ||
} | ||
current.next = current.next.next; | ||
|
||
return head; | ||
} |
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,21 @@ | ||
/** | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
|
||
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { | ||
if (!p && !q) return true; | ||
if (!p || !q) return false; | ||
Comment on lines
+16
to
+17
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. 타입스크립트 문법은 신기하네요. 첫번째는 둘 다 null인지 확인하는 부분, 두번째는 한쪽만 null인지 확인하는 if조건문 맞죠? 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. 넵 맞습니다~ |
||
if (p.val !== q.val) return false; | ||
|
||
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
} |
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 @@ | ||
/** | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
|
||
/* | ||
* Encodes a tree to a single string. | ||
*/ | ||
function serialize(root: TreeNode | null): string { | ||
if (!root) return "null"; | ||
|
||
let result = "" + root.val; | ||
|
||
result += "," + serialize(root.left); | ||
result += "," + serialize(root.right); | ||
|
||
return result; | ||
} | ||
|
||
/* | ||
* Decodes your encoded data to tree. | ||
*/ | ||
function deserialize(data: string): TreeNode | null { | ||
const values = data.split(","); | ||
let index = 0; | ||
|
||
const buildTree = () => { | ||
const val = values[index]; | ||
index++; | ||
|
||
if (val === "null") return null; | ||
|
||
const node = new TreeNode(Number(val)); | ||
node.left = buildTree(); | ||
node.right = buildTree(); | ||
return node; | ||
}; | ||
|
||
return buildTree(); | ||
} | ||
|
||
/** | ||
* Your functions will be called as such: | ||
* deserialize(serialize(root)); | ||
*/ |
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.
세심하게 변수에 대한 설명을 적어놓으신 게 인상 깊습니다.