diff --git a/non-overlapping-intervals/gwbaik9717.js b/non-overlapping-intervals/gwbaik9717.js new file mode 100644 index 000000000..3eb5bcd2e --- /dev/null +++ b/non-overlapping-intervals/gwbaik9717.js @@ -0,0 +1,35 @@ +// Time complexity: O(nlogn) +// Space complexity: O(1) + +/** + * @param {number[][]} intervals + * @return {number} + */ +var eraseOverlapIntervals = function (intervals) { + intervals.sort((a, b) => { + if (a[0] === b[0]) { + return a[1] - b[1]; + } + + return a[0] - b[0]; + }); + + let count = 0; + let prevEnd = intervals[0][1]; + + for (let i = 1; i < intervals.length; i++) { + const [start, end] = intervals[i]; + + // 구간이 겹칠 때 + if (prevEnd > start) { + count++; + prevEnd = Math.min(prevEnd, end); + continue; + } + + // 구간이 겹치지 않을 때 + prevEnd = end; + } + + return count; +}; diff --git a/remove-nth-node-from-end-of-list/gwbaik9717.js b/remove-nth-node-from-end-of-list/gwbaik9717.js new file mode 100644 index 000000000..4efdeb541 --- /dev/null +++ b/remove-nth-node-from-end-of-list/gwbaik9717.js @@ -0,0 +1,50 @@ +// Time complexity: O(n) +// Space complexity: O(1) + +/** + * 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 reverse = (head) => { + let next = null; + let current = head; + + while (current) { + const temp = current.next; + current.next = next; + next = current; + current = temp; + } + + return next; + }; + + // Reverse + let reversedHead = reverse(head); + + if (n === 1) { + reversedHead = reversedHead.next; + } else { + let prev = null; + let current = reversedHead; + + for (let i = 1; i < n; i++) { + prev = current; + current = current.next; + } + + prev.next = current.next; + } + + // Reverse Again + return reverse(reversedHead); +}; diff --git a/same-tree/gwbaik9717.js b/same-tree/gwbaik9717.js new file mode 100644 index 000000000..56ebcf505 --- /dev/null +++ b/same-tree/gwbaik9717.js @@ -0,0 +1,38 @@ +// n: number of nodes +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * 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 dfs = (currentP, currentQ) => { + if (!currentP && !currentQ) { + return true; + } + + if ((!currentP && currentQ) || (currentP && !currentQ)) { + return false; + } + + if (currentP.val !== currentQ.val) { + return false; + } + + return ( + dfs(currentP.left, currentQ.left) && dfs(currentP.right, currentQ.right) + ); + }; + + return dfs(p, q); +}; diff --git a/serialize-and-deserialize-binary-tree/gwbaik9717.js b/serialize-and-deserialize-binary-tree/gwbaik9717.js new file mode 100644 index 000000000..6237078f3 --- /dev/null +++ b/serialize-and-deserialize-binary-tree/gwbaik9717.js @@ -0,0 +1,132 @@ +// n: number of nodes, h: height of tree (max: n) +// Time complexity: O(n) +// Space complexity: O(2^h) + +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +class _Queue { + constructor() { + this.q = []; + this.left = 0; + this.right = 0; + } + + push(value) { + this.q.push(value); + this.right++; + } + + shift() { + const rv = this.q[this.left]; + delete this.q[this.left++]; + + return rv; + } + + isEmpty() { + return this.left === this.right; + } +} + +const isValid = (data) => { + if (data === undefined || data === null) { + return false; + } + + return true; +}; + +/** + * Encodes a tree to a single string. + * + * @param {TreeNode} root + * @return {string} + */ +var serialize = function (root) { + const answer = [null]; + + const bfs = (current) => { + const q = new _Queue(); + q.push([1, current]); + + while (!q.isEmpty()) { + const [i, current] = q.shift(); + + if (current === null) { + answer[i] = current; + continue; + } + + answer[i] = current.val; + + const left = 2 * i; + const right = left + 1; + + if (current.left) { + q.push([left, current.left]); + } else { + q.push([left, null]); + } + + if (current.right) { + q.push([right, current.right]); + } else { + q.push([right, null]); + } + } + }; + + bfs(root); + + while (answer.length > 1 && !isValid(answer.at(-1))) { + answer.pop(); + } + + return answer; +}; + +/** + * Decodes your encoded data to tree. + * + * @param {string} data + * @return {TreeNode} + */ +var deserialize = function (data) { + if (data.length === 1) { + return null; + } + + const root = new TreeNode(data[1]); + const q = new _Queue(); + q.push([1, root]); + + while (!q.isEmpty()) { + const [i, current] = q.shift(); + + const left = i * 2; + const right = left + 1; + + if (left <= data.length && isValid(data[left])) { + current.left = new TreeNode(data[left]); + q.push([left, current.left]); + } + + if (right <= data.length && isValid(data[right])) { + current.right = new TreeNode(data[right]); + q.push([right, current.right]); + } + } + + return root; +}; + +/** + * Your functions will be called as such: + * deserialize(serialize(root)); + */