Skip to content
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 3 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions merge-intervals/sunjae95.js
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;
};
36 changes: 36 additions & 0 deletions remove-nth-node-from-end-of-list/sunjae.js
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 해답의 공간복잡도는 O(1)인 것 같은데 혹시 선형 크기만큼 공간을 포함하는 이유가 무엇일까요??

*/
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 임시 리스트를 생성해서 참조를 다 담아서 비효율적으로 풀었었는데, 총 노드 사이즈를 구한 후에 헤드 노드의 참조를 처음부터 다시 갱신하여 n의 위치에서는 건너뛰는 방법 인상깊네요 ㅎㅎ
잘 봤습니다!


return answer.next;
};
22 changes: 22 additions & 0 deletions same-tree/sunjae95.js
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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
};