-
Notifications
You must be signed in to change notification settings - Fork 126
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
[선재] WEEK 12 Solution #567
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,28 @@ | ||
/** | ||
* @description | ||
* 각 intervals의 시작점을 기준으로 정렬 후 tmp의 end와 각 start를 비교하여 풀이 | ||
* | ||
* n = length of intervals | ||
* time complexity: O(n log n) | ||
* space complexity: O(n) | ||
*/ | ||
var merge = function (intervals) { | ||
intervals.sort((a, b) => a[0] - b[0]); | ||
|
||
const answer = []; | ||
let mergeTmp = intervals[0]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
const [start, end] = intervals[i]; | ||
|
||
if (mergeTmp[1] >= start) mergeTmp[1] = Math.max(mergeTmp[1], end); | ||
else { | ||
answer.push(mergeTmp); | ||
mergeTmp = [start, end]; | ||
} | ||
} | ||
|
||
answer.push(mergeTmp); | ||
|
||
return answer; | ||
}; |
This file contains 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 @@ | ||
/** | ||
* @description | ||
* 전체 노드의 길이를 구한 뒤 n에 적합한 노드만 건너뛰어 노드를 재배열 시켜줌 | ||
* | ||
* n = total length of head node list | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var removeNthFromEnd = function (head, n) { | ||
let node; | ||
let nodeCount = 0; | ||
|
||
node = head; | ||
while (node) { | ||
nodeCount++; | ||
node = node.next; | ||
} | ||
|
||
let answer = new ListNode(); | ||
let answerNode = answer; | ||
|
||
node = head; | ||
for (let i = 0; i < nodeCount; i++) { | ||
if (nodeCount - n === i) { | ||
i++; | ||
node = node.next; | ||
} | ||
|
||
answerNode.next = node; | ||
answerNode = node; | ||
|
||
node = node?.next ?? null; | ||
} | ||
Comment on lines
+23
to
+33
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. 저는 임시 리스트를 생성해서 참조를 다 담아서 비효율적으로 풀었었는데, 총 노드 사이즈를 구한 후에 헤드 노드의 참조를 처음부터 다시 갱신하여 n의 위치에서는 건너뛰는 방법 인상깊네요 ㅎㅎ |
||
|
||
return answer.next; | ||
}; |
This file contains 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,22 @@ | ||
/** | ||
* @description | ||
* 두개의 트리를 동시에 순회한다를 초점으로 문제접근하여 풀이 | ||
* | ||
* n = minimum tree node count of p or q | ||
* time complexity: O(n) | ||
* space complexity: O(1) | ||
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. 콜 스택이 차지하는 메모리도 고려해야하지 않을까요? |
||
*/ | ||
var isSameTree = function (p, q) { | ||
const preOrder = (tree1, tree2) => { | ||
if (!tree1 && !tree2) return true; | ||
if (!tree1 || !tree2) return false; | ||
|
||
if (tree1.val !== tree2.val) return false; | ||
if (!preOrder(tree1.left, tree2.left)) return false; | ||
if (!preOrder(tree1.right, tree2.right)) return false; | ||
|
||
return true; | ||
}; | ||
|
||
return preOrder(p, q); | ||
}; |
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.
이 해답의 공간복잡도는
O(1)
인 것 같은데 혹시 선형 크기만큼 공간을 포함하는 이유가 무엇일까요??