From 125a14c0e1c02185a4abf236660091384d25b939 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Tue, 22 Oct 2024 00:36:35 +0900 Subject: [PATCH 1/4] solve: maximum depth of binary tree --- maximum-depth-of-binary-tree/wogha95.js | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 maximum-depth-of-binary-tree/wogha95.js diff --git a/maximum-depth-of-binary-tree/wogha95.js b/maximum-depth-of-binary-tree/wogha95.js new file mode 100644 index 000000000..987cce601 --- /dev/null +++ b/maximum-depth-of-binary-tree/wogha95.js @@ -0,0 +1,46 @@ +/** + * TC: O(N) + * 모든 노드를 순회합니다. + * + * SC: O(N) + * 노드의 수에 비례해서 queue의 길이가 증가됩니다. + * + * N: count of tree node + */ + +/** + * 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} root + * @return {number} + */ +var maxDepth = function (root) { + let maximumDepth = 0; + + const queue = [[root, 1]]; + while (queue.length > 0) { + const [current, depth] = queue.shift(); + + if (current === null) { + break; + } + + maximumDepth = Math.max(maximumDepth, depth); + + if (current.left) { + queue.push([current.left, depth + 1]); + } + + if (current.right) { + queue.push([current.right, depth + 1]); + } + } + + return maximumDepth; +}; From 676d5c2614e2b22b0c6646e563a60b972a016f16 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Tue, 22 Oct 2024 21:00:25 +0900 Subject: [PATCH 2/4] solve: reorder list --- reorder-list/wogha95.js | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 reorder-list/wogha95.js diff --git a/reorder-list/wogha95.js b/reorder-list/wogha95.js new file mode 100644 index 000000000..1d293b271 --- /dev/null +++ b/reorder-list/wogha95.js @@ -0,0 +1,56 @@ +/** + * TC: O(N) + * 1번에서 절반 길이만큼 순회 + * 2번에서 절반 길이만큼 순회 + * 3번에서 절반 길이만큼 순회 + * + * SC: O(1) + * linked list의 node를 가리키는 포인터를 가지고 활용하므로 linked list의 길이와 무관한 공간 복잡도를 갖습니다. + * + * N: linked 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 + * @return {void} Do not return anything, modify head in-place instead. + */ +var reorderList = function (head) { + // 1. linked list 절반 위치 구하기 + let slow = head; + let fast = head; + + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + } + + // 2. 후반 linked list 순서 뒤집기 + let halfStartTemp = slow.next; + let halfStart = null; + // 절반을 기준으로 linked list 끊기 + slow.next = null; + + while (halfStartTemp) { + const temp = halfStartTemp.next; + halfStartTemp.next = halfStart; + halfStart = halfStartTemp; + halfStartTemp = temp; + } + + // 3. 두 리스트 합치기 + while (head && halfStart) { + const headTemp = head.next; + const halfStartTemp = halfStart.next; + head.next = halfStart; + halfStart.next = headTemp; + head = headTemp; + halfStart = halfStartTemp; + } +}; From 11bd72aadeddc7dfa684c9d8432655f02abd707e Mon Sep 17 00:00:00 2001 From: wogha95 Date: Tue, 22 Oct 2024 22:03:03 +0900 Subject: [PATCH 3/4] solve: insert interval --- insert-interval/wogha95.js | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 insert-interval/wogha95.js diff --git a/insert-interval/wogha95.js b/insert-interval/wogha95.js new file mode 100644 index 000000000..68e48a57f --- /dev/null +++ b/insert-interval/wogha95.js @@ -0,0 +1,69 @@ +/** + * TC: O(N) + * 1번에서 N만큼 순회 + * 2번에서 최대 N만큼 순회 + * + * SC: O(1) + * 겹치는 interval의 시작, 끝 index를 저장하는 변수만 사용 + * + * N: intervals.length + */ + +/** + * @param {number[][]} intervals + * @param {number[]} newInterval + * @return {number[][]} + */ +var insert = function (intervals, newInterval) { + if (intervals.length === 0) { + return [newInterval]; + } + if (newInterval[1] < intervals[0][0]) { + return [newInterval, ...intervals]; + } + if (intervals[intervals.length - 1][1] < newInterval[0]) { + return [...intervals, newInterval]; + } + + // 1. 겹치는 interval의 시작, 끝 index를 구한다. + let overLappingStartIndex = intervals.length - 1; + let overLappingEndIndex = 0; + for (let index = 0; index < intervals.length; index++) { + const interval = intervals[index]; + if ( + (interval[0] <= newInterval[0] && newInterval[0] <= interval[1]) || + newInterval[0] < interval[0] + ) { + overLappingStartIndex = Math.min(overLappingStartIndex, index); + } + if ( + (interval[0] <= newInterval[1] && newInterval[1] <= interval[1]) || + interval[1] < newInterval[1] + ) { + overLappingEndIndex = Math.max(overLappingEndIndex, index); + } + } + + // 2. + // 시작index 전까지 모두 추가하고 + // 시작index, 끝index의 최대 범위 interval을 추가하고 + // 끝index 이후 interval을 모두 추가한다. + const result = []; + for (let index = 0; index < overLappingStartIndex; index++) { + result.push(intervals[index]); + } + const overLappedStart = Math.min( + intervals[overLappingStartIndex][0], + newInterval[0] + ); + const overLappedEnd = Math.max( + intervals[overLappingEndIndex][1], + newInterval[1] + ); + result.push([overLappedStart, overLappedEnd]); + for (let index = overLappingEndIndex + 1; index < intervals.length; index++) { + result.push(intervals[index]); + } + + return result; +}; From 46a14fed774aa4d8e95e667266f840d335efc4eb Mon Sep 17 00:00:00 2001 From: pirateTurtle <75886763+wogha95@users.noreply.github.com> Date: Thu, 24 Oct 2024 22:39:23 +0900 Subject: [PATCH 4/4] Update insert-interval/wogha95.js Co-authored-by: Dale Seo <5466341+DaleSeo@users.noreply.github.com> --- insert-interval/wogha95.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/insert-interval/wogha95.js b/insert-interval/wogha95.js index 68e48a57f..c97e40158 100644 --- a/insert-interval/wogha95.js +++ b/insert-interval/wogha95.js @@ -30,16 +30,10 @@ var insert = function (intervals, newInterval) { let overLappingEndIndex = 0; for (let index = 0; index < intervals.length; index++) { const interval = intervals[index]; - if ( - (interval[0] <= newInterval[0] && newInterval[0] <= interval[1]) || - newInterval[0] < interval[0] - ) { + if (newInterval[0] <= interval[1]) { overLappingStartIndex = Math.min(overLappingStartIndex, index); } - if ( - (interval[0] <= newInterval[1] && newInterval[1] <= interval[1]) || - interval[1] < newInterval[1] - ) { + if (interval[0] <= newInterval[1]) { overLappingEndIndex = Math.max(overLappingEndIndex, index); } }