-
-
Notifications
You must be signed in to change notification settings - Fork 245
[uraflower] Week12 Solutions #1588
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
424cc78
[ PS ] : Same Tree
uraflower 2d70f08
[ PS ] : Remove Nth Node From End of List
uraflower e110a5b
[ PS ] : Number of Connected Components in an Undirected Graph
uraflower cc71d64
[ PS ] : Non Overlapping Intervals
uraflower 598857e
[ PS ] : Serialize and Deserialize Binary Tree
uraflower 4500272
시간복잡도 올바르게 수정
uraflower 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,26 @@ | ||
/** | ||
* 구간이 겹치지 않게 하기 위해 최소한의 구간을 없애야 할 때, 몇 개를 없애야 하는지 반환하는 함수 | ||
* @param {number[][]} intervals | ||
* @return {number} | ||
*/ | ||
const eraseOverlapIntervals = function(intervals) { | ||
intervals.sort((a,b) => a[0] - b[0]); | ||
|
||
let count = 0; | ||
let prevEnd = intervals[0][1]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
const [start, end] = intervals[i]; | ||
|
||
// 범위가 겹치는 경우 | ||
if (prevEnd > start) { | ||
count++; | ||
prevEnd = Math.min(prevEnd, end); // end가 큰 걸 삭제한다 치기 | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) |
47 changes: 47 additions & 0 deletions
47
number-of-connected-components-in-an-undirected-graph/uraflower.js
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,47 @@ | ||
|
||
/** | ||
* @param n: the number of vertices | ||
* @param edges: the edges of undirected graph | ||
* @return: the number of connected components | ||
*/ | ||
const countComponents = function (n, edges) { | ||
// graph 만들기 | ||
const graph = Array.from({ length: n }).map(() => []); | ||
|
||
for (const [u, v] of edges) { | ||
graph[u].push(v); | ||
graph[v].push(u); | ||
} | ||
|
||
// 각 노드 순회하기 | ||
let count = 0; | ||
const visited = new Set(); | ||
|
||
for (let i = 0; i < n; i++) { | ||
if (visited.has(i)) { | ||
continue; | ||
} | ||
|
||
count += 1; | ||
|
||
// bfs | ||
const queue = [i]; | ||
visited.add(i); | ||
|
||
while (queue.length) { | ||
const u = queue.shift(); | ||
|
||
for (const v of graph[u]) { | ||
if (!visited.has(v)) { | ||
visited.add(v); | ||
queue.push(v); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
// 시간복잡도: O(E) | ||
// 공간복잡도: O(V) |
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,59 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
|
||
// 첫 번째 풀이 | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) | ||
/** | ||
* @param {ListNode} head | ||
* @param {number} n | ||
* @return {ListNode} | ||
*/ | ||
const removeNthFromEnd = function (head, n) { | ||
function dfs(node) { | ||
// 마지막 노드이면 1 반환 | ||
if (!node.next) { | ||
return 1; | ||
} | ||
|
||
const nth = dfs(node.next); | ||
if (nth === n) { | ||
node.next = node.next.next ?? null; | ||
} | ||
|
||
return nth + 1; | ||
} | ||
|
||
if (dfs(head) === n) { | ||
return head.next; | ||
} | ||
|
||
return head; | ||
}; | ||
|
||
// 두 번째 풀이 | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) | ||
const removeNthFromEnd = function (head, n) { | ||
const temp = new ListNode(0, head); | ||
let tail = temp; | ||
let prev = temp; // 뒤에서 n번째 노드 | ||
|
||
for (let i = 0; i < n; i++) { | ||
tail = tail.next; | ||
} | ||
|
||
while (tail.next) { | ||
tail = tail.next; | ||
prev = prev.next; | ||
} | ||
|
||
prev.next = prev.next.next; | ||
|
||
return temp.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,20 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} p | ||
* @param {TreeNode} q | ||
* @return {boolean} | ||
*/ | ||
const isSameTree = function(p, q) { | ||
if (!p && !q) return true; | ||
return p?.val === q?.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(h) (h: 트리의 높이, 즉 재귀 호출 스택) |
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.
sort 때문에 전체 시간복잡도는 O(n log n)입니다.