-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #572 from wogha95/main
[재호] WEEK 12 Solutions
- Loading branch information
Showing
3 changed files
with
171 additions
and
0 deletions.
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,34 @@ | ||
/** | ||
* 정렬한 다음, 마지막 원소의 end값을 가지고 merge할지 원소 추가할지를 결정합니다. | ||
* | ||
* TC: O(N * logN) | ||
* 정렬에 의해 N * logN 복잡도를 갖습니다. | ||
* | ||
* SC: O(1) | ||
* 정답을 반환하는 공간을 제외하면 O(1), 포함하면 O(N)입니다. | ||
* | ||
* N: intervals.length | ||
*/ | ||
|
||
/** | ||
* @param {number[][]} intervals | ||
* @return {number[][]} | ||
*/ | ||
var merge = function (intervals) { | ||
intervals.sort((a, b) => a[0] - b[0]); | ||
|
||
return intervals.reduce((result, current) => { | ||
if (result.length === 0) { | ||
result.push(current); | ||
return result; | ||
} | ||
|
||
const previous = result[result.length - 1]; | ||
if (previous[1] >= current[0]) { | ||
result[result.length - 1][1] = Math.max(current[1], previous[1]); | ||
} else { | ||
result.push(current); | ||
} | ||
return result; | ||
}, []); | ||
}; |
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,89 @@ | ||
/** | ||
* 2차 | ||
* n만큼 first가 움직인 다음, first가 끝까지 갈때까지 second를 움직입니다. | ||
* 그럼 그 위치가 끝에서 n번째임을 알 수 있는 풀이입니다. | ||
* | ||
* TC: O(N) | ||
* SC: O(1) | ||
* N: list length | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @param {number} n | ||
* @return {ListNode} | ||
*/ | ||
var removeNthFromEnd = function (head, n) { | ||
const resultHead = new ListNode(null, head); | ||
let first = resultHead; | ||
let second = resultHead; | ||
|
||
while (n > 0) { | ||
first = first.next; | ||
n -= 1; | ||
} | ||
|
||
while (first.next) { | ||
first = first.next; | ||
second = second.next; | ||
} | ||
|
||
second.next = second.next.next; | ||
|
||
return resultHead.next; | ||
}; | ||
|
||
/** | ||
* 1차 | ||
* 전체 순회로 갯수를 파악하고 해당 위치로가서 링크 연결 수정 작업 | ||
* | ||
* TC: O(N) | ||
* SC: O(1) | ||
* N: list length | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @param {number} n | ||
* @return {ListNode} | ||
*/ | ||
var removeNthFromEnd = function (head, n) { | ||
let listLength = 0; | ||
let pointer = head; | ||
|
||
while (pointer) { | ||
pointer = pointer.next; | ||
listLength += 1; | ||
} | ||
|
||
// if target of removal is the first node in list | ||
if (listLength === n) { | ||
return head.next; | ||
} | ||
|
||
let nextCount = listLength - n - 1; | ||
pointer = head; | ||
|
||
while (nextCount) { | ||
pointer = pointer.next; | ||
nextCount -= 1; | ||
} | ||
|
||
pointer.next = pointer.next.next; | ||
|
||
return head; | ||
}; |
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,48 @@ | ||
/** | ||
* TC: O(N) | ||
* SC: O(N) | ||
* N: count of node in tree | ||
*/ | ||
|
||
/** | ||
* 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} | ||
*/ | ||
var isSameTree = function (p, q) { | ||
const queueP = [p]; | ||
const queueQ = [q]; | ||
|
||
while (queueP.length > 0 && queueQ.length > 0) { | ||
const currentP = queueP.shift(); | ||
const currentQ = queueQ.shift(); | ||
|
||
if (currentP === null && currentQ === null) { | ||
continue; | ||
} | ||
|
||
if (currentP === null || currentQ === null) { | ||
return false; | ||
} | ||
|
||
if (currentP.val !== currentQ.val) { | ||
return false; | ||
} | ||
|
||
queueP.push(currentP.left); | ||
queueP.push(currentP.right); | ||
|
||
queueQ.push(currentQ.left); | ||
queueQ.push(currentQ.right); | ||
} | ||
|
||
return queueP.length === 0 && queueQ.length === 0; | ||
}; |