From ce01867e144f66d02cac9c55e43f0ac16666b1d6 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Sun, 2 Mar 2025 00:20:34 +0900 Subject: [PATCH 1/8] meeting-rooms solution --- meeting-rooms/jdy8739.js | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 meeting-rooms/jdy8739.js diff --git a/meeting-rooms/jdy8739.js b/meeting-rooms/jdy8739.js new file mode 100644 index 000000000..d2aa06a1a --- /dev/null +++ b/meeting-rooms/jdy8739.js @@ -0,0 +1,44 @@ +import { + Interval, + } from '/opt/node/lib/lintcode/index.js'; + + /** + * Definition of Interval: + * class Interval { + * constructor(start, end) { + * this.start = start; + * this.end = end; + * } + * } + */ + + export class Solution { + /** + * @param intervals: an array of meeting time intervals + * @return: if a person could attend all meetings + */ + canAttendMeetings(intervals) { + const sort = intervals.sort((a, b) => { + if (a[1] === b[1]) { + return a[0] - b[0]; + } + + return a[1] - b[1]; + }) + + for (let i = 0; i < sort.length - 2; i++) { + const current = sort[i][1]; + const next = sort[i + 1][0]; + + if (current > next) { + return false; + } + } + + return true; + } + } + + // 시간복잡도 O(nlogn) -> sort 함수 사용으로 인한 시간복잡도 + // 공간복잡도 O(1) -> 사용하는 자료구조, 함수실행스택 없음 + \ No newline at end of file From bc4ccbbf87cd0c31d5260bc47f19655d546d4ce9 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Sun, 2 Mar 2025 00:28:29 +0900 Subject: [PATCH 2/8] meeting-roos line fix --- meeting-rooms/jdy8739.js | 1 + 1 file changed, 1 insertion(+) diff --git a/meeting-rooms/jdy8739.js b/meeting-rooms/jdy8739.js index d2aa06a1a..ba58bc900 100644 --- a/meeting-rooms/jdy8739.js +++ b/meeting-rooms/jdy8739.js @@ -41,4 +41,5 @@ import { // 시간복잡도 O(nlogn) -> sort 함수 사용으로 인한 시간복잡도 // 공간복잡도 O(1) -> 사용하는 자료구조, 함수실행스택 없음 + \ No newline at end of file From a2ec0d667e93a04743a9ffc4a25b25b5f7cfedc3 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Sun, 2 Mar 2025 00:29:48 +0900 Subject: [PATCH 3/8] meeting-rooms linelint --- meeting-rooms/jdy8739.js | 69 ++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/meeting-rooms/jdy8739.js b/meeting-rooms/jdy8739.js index ba58bc900..736c40d2e 100644 --- a/meeting-rooms/jdy8739.js +++ b/meeting-rooms/jdy8739.js @@ -1,45 +1,44 @@ import { - Interval, - } from '/opt/node/lib/lintcode/index.js'; - + Interval, +} from '/opt/node/lib/lintcode/index.js'; + +/** + * Definition of Interval: + * class Interval { + * constructor(start, end) { + * this.start = start; + * this.end = end; + * } + * } + */ + +export class Solution { /** - * Definition of Interval: - * class Interval { - * constructor(start, end) { - * this.start = start; - * this.end = end; - * } - * } + * @param intervals: an array of meeting time intervals + * @return: if a person could attend all meetings */ - - export class Solution { - /** - * @param intervals: an array of meeting time intervals - * @return: if a person could attend all meetings - */ - canAttendMeetings(intervals) { - const sort = intervals.sort((a, b) => { - if (a[1] === b[1]) { - return a[0] - b[0]; - } + canAttendMeetings(intervals) { + const sort = intervals.sort((a, b) => { + if (a[1] === b[1]) { + return a[0] - b[0]; + } - return a[1] - b[1]; - }) - - for (let i = 0; i < sort.length - 2; i++) { - const current = sort[i][1]; - const next = sort[i + 1][0]; + return a[1] - b[1]; + }) + + for (let i = 0; i < sort.length - 2; i++) { + const current = sort[i][1]; + const next = sort[i + 1][0]; - if (current > next) { - return false; - } + if (current > next) { + return false; } - - return true; } + + return true; } +} - // 시간복잡도 O(nlogn) -> sort 함수 사용으로 인한 시간복잡도 - // 공간복잡도 O(1) -> 사용하는 자료구조, 함수실행스택 없음 +// 시간복잡도 O(nlogn) -> sort 함수 사용으로 인한 시간복잡도 +// 공간복잡도 O(1) -> 사용하는 자료구조, 함수실행스택 없음 - \ No newline at end of file From 4d5a551e20b5ebe8370cd2de6a7f912bf6e63cbc Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Mon, 3 Mar 2025 00:01:57 +0900 Subject: [PATCH 4/8] kth-smallest-element-in-a-bst solution --- kth-smallest-element-in-a-bst/jdy8739.js | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/jdy8739.js diff --git a/kth-smallest-element-in-a-bst/jdy8739.js b/kth-smallest-element-in-a-bst/jdy8739.js new file mode 100644 index 000000000..53771dd33 --- /dev/null +++ b/kth-smallest-element-in-a-bst/jdy8739.js @@ -0,0 +1,37 @@ + /** + * 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 + * @param {number} k + * @return {number} + */ +var kthSmallest = function(root, k) { + const arr = []; + + const dfs = (node) => { + arr.push(node.val); + + if (node?.left) { + dfs(node.left); + } + + if (node?.right) { + dfs(node.right); + } + } + + dfs(root); + + const sort = arr.sort((a, b) => a - b); + + return sort[k - 1]; +}; + +// 시간복잡도 -> O(nlogn) dfs로 노드의 val을 배열에 넣고 정렬하는 시간이 소요됨 +// 공간복잡도 -> O(n) 리스트의 길이만큼 arr의 공간이 필요함 From c84c6906a194bd498dfd8a32c34b3518adff8289 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Mon, 3 Mar 2025 12:09:35 +0900 Subject: [PATCH 5/8] lowest-common-ancestor-of-a-binary-search-tree solution --- .../jdy8738.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js diff --git a/lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js b/lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js new file mode 100644 index 000000000..6e9d63e6c --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js @@ -0,0 +1,41 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function (root, p, q) { + const left = Math.min(p.val, q.val); + const right = Math.max(p.val, q.val); + + const dfs = (node) => { + if (!node) { + return; + } + + if (node.val >= left && node.val <= right) { + return node; + } + + if (node.val > left && node.val > right) { + return dfs(node.left); + } + + if (node.val < left && node.val < right) { + return dfs(node.right); + } + } + + return dfs(root); +}; + +// 시간복잡도 O(logn) -> 이진트리를 이진탐색하면서 트리의 노드를 방문하기떄문(동일한 깊이에서 한 번 왼쪽을 방문하였다면, 그 깊이에서 오른쪽을 방문하지 않음) +// 공간복잡도 O(h) -> 재귀호출을 사용하였으므로 트리의 높이만큼 최대 콜스택의 쌓임이 발생 From 1f862bbcd69beb6b210b48a03a2eaadb5b824d2b Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Mon, 3 Mar 2025 12:15:08 +0900 Subject: [PATCH 6/8] lowest-common-ancestor-of-a-binary-search-tree solution --- lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js b/lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js index 6e9d63e6c..92f3b7249 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js +++ b/lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js @@ -39,3 +39,4 @@ var lowestCommonAncestor = function (root, p, q) { // 시간복잡도 O(logn) -> 이진트리를 이진탐색하면서 트리의 노드를 방문하기떄문(동일한 깊이에서 한 번 왼쪽을 방문하였다면, 그 깊이에서 오른쪽을 방문하지 않음) // 공간복잡도 O(h) -> 재귀호출을 사용하였으므로 트리의 높이만큼 최대 콜스택의 쌓임이 발생 + From 4d203e9ce2bb7d34afe825bd4a16b8eb8dc31052 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Mon, 3 Mar 2025 12:16:31 +0900 Subject: [PATCH 7/8] lowest-common-ancestor-of-a-binary-search-tree solution --- .../{jdy8738.js => jdy8739.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lowest-common-ancestor-of-a-binary-search-tree/{jdy8738.js => jdy8739.js} (100%) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js b/lowest-common-ancestor-of-a-binary-search-tree/jdy8739.js similarity index 100% rename from lowest-common-ancestor-of-a-binary-search-tree/jdy8738.js rename to lowest-common-ancestor-of-a-binary-search-tree/jdy8739.js From 98398cc421d73bab72f347da3ed63d938e439746 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Mon, 3 Mar 2025 12:31:12 +0900 Subject: [PATCH 8/8] lowest-common-ancestor-of-a-binary-search-tree solution2 --- .../jdy8739.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/jdy8739.js b/lowest-common-ancestor-of-a-binary-search-tree/jdy8739.js index 92f3b7249..8dfa8d38f 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/jdy8739.js +++ b/lowest-common-ancestor-of-a-binary-search-tree/jdy8739.js @@ -40,3 +40,37 @@ var lowestCommonAncestor = function (root, p, q) { // 시간복잡도 O(logn) -> 이진트리를 이진탐색하면서 트리의 노드를 방문하기떄문(동일한 깊이에서 한 번 왼쪽을 방문하였다면, 그 깊이에서 오른쪽을 방문하지 않음) // 공간복잡도 O(h) -> 재귀호출을 사용하였으므로 트리의 높이만큼 최대 콜스택의 쌓임이 발생 +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function (root, p, q) { + let node = root; + + while (node) { + if (node.val >= p.val && node.val <= q.val) { + return node; + } else if (node.val < p.val && node.val < q.val) { + node = node.right; + } else if (node.val > p.val && node.val > q.val) { + node = node.left; + } else { + break; + } + } + + return node; +}; + +// 시간복잡도 O(logn) -> 이진트리를 이진탐색하면서 트리의 노드를 방문하기떄문(동일한 깊이에서 한 번 왼쪽을 방문하였다면, 그 깊이에서 오른쪽을 방문하지 않음) +// 공간복잡도 O(1) -> 알고리즘을 위해 요구되는 자료구조나 콜스택의 쌓임이 발생하지 않음