From e521fd6f1ee2cfdc929416e8c70c0006e925a339 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sat, 2 Nov 2024 14:01:48 +0900 Subject: [PATCH 1/5] solve: same tree --- same-tree/wogha95.js | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 same-tree/wogha95.js diff --git a/same-tree/wogha95.js b/same-tree/wogha95.js new file mode 100644 index 000000000..1249e6bb4 --- /dev/null +++ b/same-tree/wogha95.js @@ -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; +}; From 9ae0b362945c6cc3bbf0b72cf63e7def5f27ef87 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sat, 2 Nov 2024 14:19:46 +0900 Subject: [PATCH 2/5] sovle: remove nth node from end of list --- remove-nth-node-from-end-of-list/wogha95.js | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/wogha95.js diff --git a/remove-nth-node-from-end-of-list/wogha95.js b/remove-nth-node-from-end-of-list/wogha95.js new file mode 100644 index 000000000..022a360d5 --- /dev/null +++ b/remove-nth-node-from-end-of-list/wogha95.js @@ -0,0 +1,44 @@ +/** + * 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; +}; From 79e104a509812000087bc2e271b56f46d6bca343 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sat, 2 Nov 2024 14:26:59 +0900 Subject: [PATCH 3/5] solve: remove nth node from end of list --- remove-nth-node-from-end-of-list/wogha95.js | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/remove-nth-node-from-end-of-list/wogha95.js b/remove-nth-node-from-end-of-list/wogha95.js index 022a360d5..60c834794 100644 --- a/remove-nth-node-from-end-of-list/wogha95.js +++ b/remove-nth-node-from-end-of-list/wogha95.js @@ -1,4 +1,49 @@ /** + * 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 From 33f248e7a354336664e00fe7e1e5b7bc430ba726 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sat, 2 Nov 2024 14:41:41 +0900 Subject: [PATCH 4/5] solve: merge intervals --- merge-intervals/wogha95.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 merge-intervals/wogha95.js diff --git a/merge-intervals/wogha95.js b/merge-intervals/wogha95.js new file mode 100644 index 000000000..d720ea731 --- /dev/null +++ b/merge-intervals/wogha95.js @@ -0,0 +1,32 @@ +/** + * 정렬한 다음, 마지막 원소의 end값을 가지고 merge할지 원소 추가할지를 결정합니다. + * + * TC: O(N * logN) + * 정렬에 의해 N * logN 복잡도를 갖습니다. + * + * SC: O(1) + * 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; + }, []); +}; From a1a9a8c7b23696d26d34917cff3123db316d6c29 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sat, 2 Nov 2024 14:43:18 +0900 Subject: [PATCH 5/5] solve: merge intervals --- merge-intervals/wogha95.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/merge-intervals/wogha95.js b/merge-intervals/wogha95.js index d720ea731..062850a16 100644 --- a/merge-intervals/wogha95.js +++ b/merge-intervals/wogha95.js @@ -5,6 +5,8 @@ * 정렬에 의해 N * logN 복잡도를 갖습니다. * * SC: O(1) + * 정답을 반환하는 공간을 제외하면 O(1), 포함하면 O(N)입니다. + * * N: intervals.length */