diff --git a/merge-intervals/sunjae95.js b/merge-intervals/sunjae95.js new file mode 100644 index 000000000..6aaaaf8ae --- /dev/null +++ b/merge-intervals/sunjae95.js @@ -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; +}; diff --git a/remove-nth-node-from-end-of-list/sunjae.js b/remove-nth-node-from-end-of-list/sunjae.js new file mode 100644 index 000000000..57951855c --- /dev/null +++ b/remove-nth-node-from-end-of-list/sunjae.js @@ -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; + } + + return answer.next; +}; diff --git a/same-tree/sunjae95.js b/same-tree/sunjae95.js new file mode 100644 index 000000000..70bceb335 --- /dev/null +++ b/same-tree/sunjae95.js @@ -0,0 +1,22 @@ +/** + * @description + * 두개의 트리를 동시에 순회한다를 초점으로 문제접근하여 풀이 + * + * n = minimum tree node count of p or q + * time complexity: O(n) + * space complexity: O(1) + */ +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); +};