From 45d187d1bb64ac623d4341de81a741bae527729f Mon Sep 17 00:00:00 2001 From: wogha95 Date: Tue, 19 Nov 2024 15:25:01 +0900 Subject: [PATCH 1/4] solve: subtree of another tree --- subtree-of-another-tree/wogha95.js | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 subtree-of-another-tree/wogha95.js diff --git a/subtree-of-another-tree/wogha95.js b/subtree-of-another-tree/wogha95.js new file mode 100644 index 000000000..cfa24661d --- /dev/null +++ b/subtree-of-another-tree/wogha95.js @@ -0,0 +1,65 @@ +/** + * tree를 순회하면서 subRoot의 시작값과 동일한 노드를 찾습니다. + * 찾으면 동일한 트리인지 확인합니다. + * + * TC: O(N) + * 최악의 경우, root tree의 모든 node를 순회합니다. + * + * SC: O(N) + * 최악의 경우, root tree의 모든 node를 순회하기 위해 queue를 사용합니다. + */ + +/** + * 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 {TreeNode} subRoot + * @return {boolean} + */ +var isSubtree = function (root, subRoot) { + const queue = [root]; + + while (queue.length > 0) { + const current = queue.shift(); + + if (!current) { + continue; + } + + if (current.val === subRoot.val && isSameTree(current, subRoot)) { + return true; + } + + if (current.left) { + queue.push(current.left); + } + + if (current.right) { + queue.push(current.right); + } + } + + return false; + + function isSameTree(rootA, rootB) { + if (rootA === null && rootB === null) { + return true; + } + + if (rootA === null || rootB === null) { + return false; + } + + return ( + rootA.val === rootB.val && + isSameTree(rootA.left, rootB.left) && + isSameTree(rootA.right, rootB.right) + ); + } +}; From a01fa0afdd720057ae21031ca697560cb6fbd9e6 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Tue, 19 Nov 2024 16:56:15 +0900 Subject: [PATCH 2/4] solve: validate binary search tree --- validate-binary-search-tree/wogha95.js | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 validate-binary-search-tree/wogha95.js diff --git a/validate-binary-search-tree/wogha95.js b/validate-binary-search-tree/wogha95.js new file mode 100644 index 000000000..05b685c12 --- /dev/null +++ b/validate-binary-search-tree/wogha95.js @@ -0,0 +1,40 @@ +/** + * TC: O(N) + * SC: O(N) + * N: total count of tree nodes + */ + +/** + * 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 {boolean} + */ +var isValidBST = function (root) { + return isValidBSTWithBoundary( + root, + Number.MIN_SAFE_INTEGER, + Number.MAX_SAFE_INTEGER + ); + + function isValidBSTWithBoundary(current, min, max) { + if (!current) { + return true; + } + + if (current.val <= min || max <= current.val) { + return false; + } + + return ( + isValidBSTWithBoundary(current.left, min, current.val) && + isValidBSTWithBoundary(current.right, current.val, max) + ); + } +}; From 760ced5501b50a79b658354e600561a1d77cbca1 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Wed, 20 Nov 2024 14:31:44 +0900 Subject: [PATCH 3/4] solve: longest palindromic substring --- longest-palindromic-substring/wogha95.js | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 longest-palindromic-substring/wogha95.js diff --git a/longest-palindromic-substring/wogha95.js b/longest-palindromic-substring/wogha95.js new file mode 100644 index 000000000..2953e113d --- /dev/null +++ b/longest-palindromic-substring/wogha95.js @@ -0,0 +1,38 @@ +/** + * TC: O(N^2) + * 주어진 s 문자열이 한 종류의 문자로 이루어져있다면 for문에서 O(N), while문에서 O(N) 이므로 O(N * 2N) + * + * SC: O(1) + */ + +/** + * @param {string} s + * @return {string} + */ +var longestPalindrome = function (s) { + let result = ""; + + for (let index = 0; index < s.length; index++) { + const [start1, end1] = getPalindromicSubstringLength(index, index); + const [start2, end2] = getPalindromicSubstringLength(index, index + 1); + + if (result.length < end1 - start1 + 1) { + result = s.substring(start1, end1 + 1); + } + + if (result.length < end2 - start2 + 1) { + result = s.substring(start2, end2 + 1); + } + } + + return result; + + function getPalindromicSubstringLength(start, end) { + while (0 <= start && end < s.length && s[start] === s[end]) { + start -= 1; + end += 1; + } + + return [start + 1, end - 1]; + } +}; From 0d2da3d088adf0544281e0acb631073987cdf237 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Wed, 20 Nov 2024 16:10:27 +0900 Subject: [PATCH 4/4] solve: rotate image --- rotate-image/wogha95.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 rotate-image/wogha95.js diff --git a/rotate-image/wogha95.js b/rotate-image/wogha95.js new file mode 100644 index 000000000..8753978ea --- /dev/null +++ b/rotate-image/wogha95.js @@ -0,0 +1,27 @@ +/** + * 전치(행과 열을 교환) 후 행 반전(뒤집기) + * + * TC: O(N^2) + * SC: O(1) + */ + +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var rotate = function (matrix) { + const N = matrix.length; + + for (let row = 0; row < N; row++) { + for (let column = row; column < N; column++) { + [matrix[row][column], matrix[column][row]] = [ + matrix[column][row], + matrix[row][column], + ]; + } + } + + for (let row = 0; row < N; row++) { + matrix[row].reverse(); + } +};